Reputation: 13
I am trying to upload a csv file to the server. Below is my code in html:
<form method="post" id="uploadCSV" enctype="multipart/form-data">
File to upload: <input type="file" id="uploadfile" name="file" accept="text/csv">
<input type="submit" value="Upload" id="uploadcsv" ng-click="uploadCSV()"> Press here to upload the file!
</form>
And my JS:-
$scope.uploadCSV = function()
{
var fileToLoad = document.getElementById("uploadfile").files[0];
var csvreporturl = "/api/oel/csv";
$http.post(csvreporturl, fileToLoad).then(function(response, status) {
console.log("posted:",response);
});
}
Finally the controller in Spring Boot:-
@RequestMapping(value = "/api/oel/csv", method = RequestMethod.POST)
String uploadFileHandler(@RequestBody MultipartFile fileToLoad) throws FileNotFoundException, IOException
{
String name = fileToLoad.getName();
if (!fileToLoad.isEmpty()) {
try {
byte[] bytes = fileToLoad.getBytes();
// Creating the directory to store file
//String rootPath = System.getProperty("catalina.home");
File dir = new File("../dashboard-0.0.12/oel");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath()
+ File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
return "You successfully uploaded file=" + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name
+ " because the file was empty.";
}
}
I am facing below error:-
Failed to load resource: the server responded with a status of 500 (HTTP/1.1 500)
Possibly unhandled rejection: {"data":{"timestamp":1510643953084,"status":500,"error":"Internal Server Error","exception":"java.lang.NullPointerException","message":"No message available","path":"/api/oel/csv"},"status":500,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/oel/csv","data":{},"headers":{"Accept":"application/json, text/plain, /","Content-Type":"application/json;charset=utf-8"}},"statusText":"HTTP/1.1 500"}
Could someone please help?
Thanks
Upvotes: 0
Views: 868
Reputation: 199
I would recommend this : Apache Commons FileUpload
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factoryItem = new DiskFileItemFactory();
ServletFileUpload uploadFile = new ServletFileUpload(factoryItem);
try {
List items = uploadFile.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: -1