我试图将选定的文件从本地文件系统上传到应用程序中的文件夹.这里使用的技术是jsp,servlet.
单击Example.jsp的Press按钮,控件将传递给Request servlet.但检查如果请求是多部分则返回false.所以"抱歉这个Servlet只处理文件上传请求"打印在控制台中.
Example.jsp
<form action="Request" method="Post" name = "invoices" target="_self">
<div class="invoicetable">
<table>
<colgroup>
<col class="first" span="1">
<col class="rest" span="1">
</colgroup>
<tr>
<td>Start date:</td>
<td><input type = "text" name = "start_date" id="datepicker" size = "6" maxlength="10"/></td>
</tr>
<tr>
<td>End date:</td>
<td><input type = "text" name = "end_date" id="datepicker2" size = "6" maxlength="10"/></td>
</tr>
<tr>
<form action="Request" enctype="multipart/form-data" method="post" name = "upload">
<table>
<tr>
<td>Input File:</td>
<td><input type = "file" name ="datafile" size="30" maxlength="200"/></td>
</tr>
</table>
<div>
<input type ="submit" name="invoice" value="Press"/> to upload the file!
<input type="hidden" name = "type" value="upload">
</div>
</form>
</tr>
<tr>
<td>Regular</td>
<td>
<input type = "radio" name ="input_type" value="regular"/>
</td>
</tr>
<tr>
<td>Manual</td>
<td>
<input type = "radio" name ="input_type" value="manual" checked/>
</td>
</tr>
<tr>
<td>Final</td>
<td>
<input type = "radio" name ="input_type" value="final"/>
</td>
</tr>
</table>
</div>
<div style="margin-left: 20px">
<input type ="submit" name="invoice" value="Submit" class="button"/>
<input type="hidden" name = "type" value="invoice">
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
请求 - doPost方法
jsp_request = request.getParameter("type");
if (jsp_request.equalsIgnoreCase("upload")) {
String file_location = request.getParameter("datafile");
ServletFileUpload uploader = null;
//Checking if the request is multipart
if(ServletFileUpload.isMultipartContent(request)){
try {
String upload_location_holder = request.getServletContext().getRealPath("");
upload_location_holder = upload_location_holder.substring(0,upload_location_holder.indexOf(".")) +
upload_location_holder.substring(upload_location_holder.lastIndexOf("/")+1);
//excel file goes into the input files folder
String excel_name = upload_location_holder + "/WebContent/input_files/" + file_location;
File file = new File(excel_name);
DiskFileItemFactory fileFactory=new DiskFileItemFactory();
fileFactory.setRepository(file);
uploader = new ServletFileUpload(fileFactory);
List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while (fileItemsIterator.hasNext()) {
FileItem item = (FileItem) fileItemsIterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
//File uploaded successfully
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
// request.setAttribute("message", "File Upload Failed due to " + ex);
ex.printStackTrace();
}
}else{
System.out.println("Sorry this Servlet only handles file upload request");
}
}
Run Code Online (Sandbox Code Playgroud)
我试着谷歌搜索可能的解决方案.但没有任何答案.任何帮助赞赏.
亲爱的,请enctype="multipart/form-data"在您的表单标签中使用:
form action="Request" method="Post" name="logout" enctype="multipart/form-data"
Run Code Online (Sandbox Code Playgroud)