2

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.

  • Does any form posting work? Maybe worth checking your security profile? – Scary Wombat Oct 01 '13 at 08:19
  • Do you have write access to the directory on EC2 where the file's expecting to be uploaded to? – David Oct 01 '13 at 08:20
  • Unfortunately this is the only form post. Ill double check the security profile. The file is saved to mysql server. But ill see if the tmp folder might be the culprit. Thanks for the ideas! – Siim Plangi Oct 01 '13 at 08:25
  • Maybe some part of your setup is parsing the request body before it gets to the servlet, see http://stackoverflow.com/questions/13048939/file-upload-with-servletfileupload-parserequest?rq=1 with a similar problem. – Udo Klimaschewski Oct 01 '13 at 08:33

0 Answers0