Reputation: 21
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);
}
}
Upvotes: 1
Views: 2900
Reputation: 10953
As the problem only occurs with files bigger than 100 KB you are running into a limit imposed by your application server.
Normally a rather low limit is set in order to keep "malicious" users from uploading files too big in size. You have to raise this maximum POST limit in order to get bigger files through.
The setting depends on the application server / servlet container you use.
If you want to change the value directly in the server settings and your server is Tomcat, have a look here: http://tomcat.apache.org/tomcat-8.0-doc/config/http.html
You can define the same value in your 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>
It is even possible to raise the value from code:
@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
(both taken from https://docs.oracle.com/javaee/6/tutorial/doc/gmhal.html)
If your code is based on Spring Boot there is an alternative to this which only needs you to set a configuration value in application.properties. This setting should work for all containers supported by Spring Boot (Tomcat 7-8, Jetty 8-9, Undertow)
multipart.maxFileSize: 128KB
multipart.maxRequestSize: 128KB
(this time taken from https://spring.io/guides/gs/uploading-files/)
Upvotes: 1