文件上载在部署到localhost时有效,但在EC2上时则无效

时间:2013-10-01 08:11:29

标签: java tomcat file-upload amazon-web-services amazon-ec2

我有问题让文件上传工作。它在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不接受。

0 个答案:

没有答案