javascript - 上传超过 100 KB 的文件时出现 500 内部服务器错误

标签 javascript java html

index.html

id="myFile" enclosed in div tag

<div id = "myFile">

</div>

Controller .js

    $("#myFile").uploadFile({
        url:/Spot_audit/upload/"+id+"/"+year",
        fileName:$scope.myFile,
        allowedTypes: "zip,docx,xlsx,7z",
            maxFileSize:5*1024*1024
    });

Controller .java

/*   file upload    */    

@RequestMapping(value="/upload/{Id}/{fy}", method=RequestMethod.POST)
public void handleFileUpload (@RequestParam("file") MultipartFile file, @PathVariable("fy") String year, @PathVariable("Id") String aid){                   
    if (!file.isEmpty()) {
        String path1="C:/Users/Downloads/"+month+"/"+aid+"/"+file.getOriginalFilename();

        Path path= Paths.get(path1);

        try{
                File newFile1 = new File(path);
            newFile1.getParentFile().mkdirs();
            newFile1.createNewFile();
            BufferedOutputStream os1 = new BufferedOutputStream(new FileOutputStream(newFile1));

            os1.write(file.getBytes(), 0, (int) file.getSize());
            os1.flush();
            os1.close();
        }catch(Exception e){
            e.printStackTrace();
            CompPath="";            
        }           
    }

    //return new ResponseEntity<String>(fullpath, HttpStatus.CREATED); 
}

}

最佳答案

由于该问题仅发生在大于 100 KB 的文件上,因此您遇到了应用程序服务器施加的限制。

通常会设置一个相当低的限制,以防止“恶意”用户上传太大的文件。您必须提高此最大 POST 限制才能通过更大的文件。

该设置取决于您使用的应用程序服务器/servlet 容器。

如果您想直接在服务器设置中更改该值并且您的服务器是 Tomcat,请查看此处: http://tomcat.apache.org/tomcat-8.0-doc/config/http.html

您可以在 web.xml 中定义相同的值:

<multipart-config>
    <location>/tmp</location>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

甚至可以从代码中提高值:

@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024, 
    maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)

(均取自 https://docs.oracle.com/javaee/6/tutorial/doc/gmhal.html )

如果您的代码基于 Spring Boot,则还有一种替代方法,只需要您在 application.properties 中设置配置值。此设置应该适用于所有 containers supported通过 Spring Boot(Tomcat 7-8、Jetty 8-9、Undertow)

multipart.maxFileSize: 128KB
multipart.maxRequestSize: 128KB

(这次取自 https://spring.io/guides/gs/uploading-files/ )

关于javascript - 上传超过 100 KB 的文件时出现 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33298420/

相关文章:

javascript - 如何(使用 React JS Web)和 Firestore,您能否找出聊天室(在 Firestore 数据库上)何时收到新消息?

javascript - 隐藏和显示跨度

java - Repaint 和 Mouselister Java

java - 无法编辑项目中的特定 webdynpro java View

java - Jersey 客户端获取错误消息正文

html - 显示列表项 css 的问题

javascript - 在构造函数中声明变量有什么用

javascript - HTML/Javascript 等待图形绘制

javascript - 如何使用javascript创建上一个按钮?

html - 有没有 HTML 到 WikiText 的翻译器?