我正在使用Eclipse做一些Java Web应用程序,并将.war文件托管在另一台PC的glassfish4服务器上。
我已经编写了一个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是更正确的方法。
答案 0 :(得分:0)
因此,数据库中的文件需要保存在Blob或其他特定于db的大字段实现中,并且在服务器上,您只需创建文件系统,然后将路径保存在数据库中即可。这是我的首选方法,因为使用该表时数据库保持响应状态。
如果您在本地运行服务器,则只需授予对计算机文件系统的访问权限,以便可以将文件保存在该文件系统中。
使用Java在服务器上保存文件的示例为
How do I create a file and write to it in Java?
https://stackabuse.com/reading-and-writing-files-in-java/
在进行实施之前,我可以建议您对Java和文件系统有很好的了解。