I have a problem to get the file uploading to work. It works perfectly on localhost, but when deployed to EC2 it just does not receive the multipart content. The fields.size() return 0 in ec2 tomcat console but 1 in localhost. Im using TOMCAT 7, Apache fileupload with io and (mysql, although I think this is currently not relevant). All the libraries are both in Tomcat lib folder and in WEB-INF/lib
Heres the code for servlet which handles the upload. I skipped the end, because it would be too long and is unrelevant because it never enters 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");
And heres the form Im using to upload
<form id = "uploadform" method="post" enctype="multipart/form-data">
<input type="file" name = "file">
<button>Saada hindamisele</button>
</form>
If anything else is needed, let me know.
EDIT: Turned out that I had the stupidest mistake there can be.
@MultipartConfig(location = "tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024, maxRequestSize = 1024 * 1024 * 2)
Im developing in windows and ec2 has ubuntu. windows accepts location = "/tmp" and ofcourse linux does not.