我有问题让文件上传工作。它在localhost上完美运行,但是当部署到EC2时,它只是不接收多部分内容。 fields.size()在ec2 tomcat控制台中返回0,但在localhost中返回1。我使用TOMCAT 7,Apache fileupload与io和(mysql,虽然我认为这目前不相关)。所有库都在Tomcat lib文件夹和WEB-INF/lib
继承处理上传的servlet代码。我跳过了最后,因为它太长而且不相关,因为它永远不会进入if(it.hasnext()
。
// References: http://docs.oracle.com/javaee/6/tutorial/doc/glrbb.html
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int taskid = Integer.parseInt(request.getParameter("id"));
boolean isMultipartContent = ServletFileUpload
.isMultipartContent(request);
if (!isMultipartContent) {
response.sendRedirect("/automaatnehindaja/taskview.html?id=" + taskid +"&result=incorrect");
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
Iterator<FileItem> it = fields.iterator();
System.out.println(fields.size());
}
if (it.hasNext()) {
Connection c = null;
PreparedStatement stmt = null;
FileItem fileitem = it.next();
if (!fileitem.getName().endsWith(".py")){
//TODO other language support
response.sendRedirect("/automaatnehindaja/taskview.html?id=" + taskid +"&result=incorrect");
return;
}
if (fileitem.getSize()>1024*1024){
response.sendRedirect("/automaatnehindaja/taskview.html?id=" + taskid +"&result=toolarge");
return;
}
Class.forName("com.mysql.jdbc.Driver");
继续使用我上传的表格
<form id = "uploadform" method="post" enctype="multipart/form-data">
<input type="file" name = "file">
<button>Saada hindamisele</button>
</form>
如果还有其他需要,请告诉我。
编辑:原来我可能有最愚蠢的错误。
@MultipartConfig(location = "tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024, maxRequestSize = 1024 * 1024 * 2)
我在windows和ec2开发有ubuntu。 windows接受location =“/ tmp”,当然linux不接受。