2
我想从本地文件系统上传一个选定的文件到应用程序内的文件夹。 这里使用的技术是jsp,servlet。Servlet文件上传ismultipartcontent返回false
单击Example.jsp的按钮后,控件传递给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>
请求 - 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");
}
}
我试图使用Google为可能的解决方案。但没有得到任何答案。 任何帮助表示赞赏。
我已经在内部形式使用ENCTYPE = “的multipart/form-data的”(上传) –
@EvesMary的问题是,你有一种“内在”形式;你不能在另一个'
@AnthonyGrist:ohh..ok..i wil在删除内部表单后更新你.. –