簡體   English   中英

JavaEE如何將文件上傳到glassfish4服務器?

[英]JavaEE how to upload file to glassfish4 server?

我正在使用eclipse做一些java web應用程序,並且我在其他PC上的glassfish4服務器上托管.war文件。

我編寫了一個jsp頁面,jsp頁面需要用戶輸入所有類型的信息並瀏覽任何類型的文件(如txt或圖像文件)並上傳到glassfish4本地服務器。

我在Google上做了很多搜索,我找不到任何解決方案,你能幫我解決這些問題嗎?

這是我的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本地服務器,並將路徑保存到數據庫中
上傳到數據庫后,我想檢索文件,讓其他用戶下載它。

我想知道哪1更容易實現? 哪1是更正確的方法。

因此,數據庫中的文件需要保存在blob或其他特定於db的大型字段實現中,並且在服務器上,您可以創建文件系統,然后將路徑保存在數據庫中。 這是我首選的方法,因為db在使用該表時保持響應。

如果您在本地運行服務器,則只需授予對計算機文件系統的訪問權限,以便可以將文件保存在那里。

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

在服務器上保存上傳的文件

如何創建文件並用Java寫入?

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

在完成實施之前,我是否可以建議您對Java和文件系統有一個很好的閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM