【问题标题】:JavaEE how to upload file to glassfish4 server?JavaEE如何将文件上传到glassfish4服务器?
【发布时间】:2019-06-20 08:00:41
【问题描述】:

我正在使用 eclipse 做一些 java web 应用程序,并将 .war 文件托管在其他 PC 的 glassfish4 服务器上。

我写了一个jsp页面,jsp页面需要用户输入各种信息,浏览任何一种文件(比如txt,或者图片文件),然后上传到glassfish4本地服务器。

我在谷歌上做了很多搜索,但我找不到任何解决方案,你们能帮我解决这些问题吗?

这是我的 JSP 页面代码:

<form action="uploadFile" method="POST" enctype="multipart/form-data">
                            <div class="card-body">
                                <h5 class="card-title">Equipment Request</h5>
                                <div class="form-group row">
                                    <label for="reqEmail" class="col-sm-2 text-right control-label col-form-label">Requestor</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="reqEmail" name="reqEmail" disabled value="reggiezhiyi.lim@motorolasolutions.com">
                                    </div>
                                    <label for="ID" class="col-sm-1 text-right control-label col-form-label">Request ID</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="ID" name="ID" disabled value="0000001">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="projectName" class="col-sm-2 text-right control-label col-form-label">Project</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="projectName" name="projectName" maxlength="50" placeholder="Enter the Project Name..">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="contactNum" class="col-sm-2 text-right control-label col-form-label">Contact No.</label>
                                    <div class="col-sm-3">
                                        <input type="number" class="form-control" id="contactNum" name="contactNum" maxlength="50" placeholder="Enter the Phone Number">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="manID" class="col-sm-2 text-right control-label col-form-label">Manager Core ID</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" id="manID" name="manID" maxlength="50" placeholder="Enter the Manager Core ID">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="from" class="col-sm-2 text-right control-label col-form-label">Loan Date From</label>
                                    <div class="col-sm-4 input-group">
                                        <input type="text" class="form-control" id="datepicker-autoclose" placeholder="mm/dd/yyyy">
                                        <div class="input-group-append">
                                            <span class="input-group-text"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                    <label for="to" class="col-sm-1 text-right control-label col-form-label">To</label>
                                    <div class="col-sm-4 input-group">
                                        <input type="text" class="form-control" id="datepicker-autoclose1" placeholder="mm/dd/yyyy">
                                        <div class="input-group-append">
                                            <span class="input-group-text"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="reason" class="col-sm-2 text-right control-label col-form-label">Reason for Loan</label>
                                    <div class="col-sm-6">
                                        <textarea rows="3" class="form-control" id="reason" name="reason"></textarea>
                                    </div> 
                                </div>
                                <div class="form-group row">
                                    <label for="attach" class="col-sm-2 text-right control-label col-form-label">Attachment</label>
                                    <div class="col-sm-6">
                                        <div class="custom-file">
                                            <label class="custom-file-label" for="validatedCustomFile" placeholder="Choose File.."></label>
                                            <input type="file" class="custom-file-input" id="validatedCustomFile" name="validatedCustomFile" required>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="comment" class="col-sm-2 text-right control-label col-form-label">Equiment Configuration</label>
                                    <div class="col-sm-9">
                                        <textarea rows="5" class="form-control" id="comment" name="comment"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="shipping" class="col-sm-2 text-right control-label col-form-label">Shipping Address</label>
                                    <div class="col-sm-9">
                                        <textarea rows="3" class="form-control" id="shipping" name="shipping"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="return" class="col-sm-2 text-right control-label col-form-label">Return Address</label>
                                    <div class="col-sm-9">
                                        <textarea rows="3" class="form-control" id="return" name="return" disabled>Motorola Solutions Penang</textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="inven" class="col-sm-2 text-right control-label col-form-label">Inventory List</label>
                                    <div class="col-sm-8"></div>
                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-primary" onclick="addTable()">Add</button>
                                        <button type="button" class="btn btn-danger" onclick="deleteTable()">Delete</button>
                                    </div>
                                    <br>

                                    <!-- <div class="col-sm-2"></div> -->
                                    <div class="col-sm-12">
                                        <table class="table" id="tableList">
                                            <thead>
                                                <tr>
                                                <th scope="col">Category</th>
                                                <th scope="col">Item</th>
                                                <th scope="col">Sales Model No.</th>
                                                <th scope="col">Asset Tag</th>
                                                <th scope="col">Serial No.</th>
                                                <th scope="col">Quantity</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="border-top">
                                <div class="card-body">
                                    <button type="submit" class="btn btn-primary">Submit</button>
                                    <button type="button" id="test" class="btn btn-primary">Test</button>
                                    <button type="reset" class="btn btn-info">Reset</button>
                                </div>
                            </div>
                        </form>

该动作直接指向一个uploadFile.java,我不知道如何将文件上传到数据库中。

我有几个关于上传到数据库的问题:
1.直接上传文件到数据库
2.上传文件到glassfish4本地服务器,并保存路径到数据库
上传到数据库后,我想检索文件,让其他用户下载。

我想知道哪一个更容易实现?哪一个是更正确的方法。

【问题讨论】:

    标签: jsp jakarta-ee glassfish-4


    【解决方案1】:

    因此,数据库中的文件需要保存在 blob 或其他一些特定于数据库的大字段实现中,而在服务器上,您可以只创建一个文件系统,然后将路径保存在数据库中。这是我的首选方法,因为数据库在使用该表时会保持响应。

    如果您在本地运行服务器,则只需授予对计算机文件系统的访问权限,以便将文件保存在那里。

    使用 java 在服务器上保存文件的示例是;

    save uploaded file at server

    How do I create a file and write to it in Java?

    https://stackabuse.com/reading-and-writing-files-in-java/

    我可以建议您在实施实施之前好好阅读一下 java 和文件系统。

    【讨论】:

    • 您好,感谢您的建议,我将通读所有文档。上次我在做 Django 时,我也曾经保存文件路径并将文件存储在服务器中,您提供的第一个链接,文件类型是否重要?或者任何文件类型都可以??
    • 我想要实现的方式不是读/写文件,只需要直接上传用户选择的文件,直接上传到服务器即可。并且在未来,文件可以从用户那里下载回来,仅此而已。
    • 要“上传”,首先需要读取文件,然后写入文件,这就是java的工作原理。您创建一个流,然后从该流写入磁盘。然后下载,同样的,但反过来。如果答案有帮助,请标记为解决方案,您应该从这些示例中获得所需的一切。
    猜你喜欢
    • 2017-04-25
    • 2011-09-25
    • 2020-03-17
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2020-01-30
    • 2019-05-24
    相关资源
    最近更新 更多