My SpringBoot API receives multipart file. Then using RestTemplate I am sending same to different API. It works fine!!!
Problem here is its storing multipart file on server harddisk. My API should just act as an intermediate service between frontend and another API endpoint.
I do not want to store received file locally
One thing I can do is delete after uploaded to different API.
Is there any better way we do same thing i.e. without storing on server or automatic delete from server.
My code is as below
Controller
#PostMapping(value = "/uploadFile", consumes = { "multipart/form-data" })
public SomePojoObject uploadFile(#RequestParam("file") MultipartFile mFile,
#RequestParam("metaData") String metaData) {
return service.uploadFile(mFile, metaData);
}
And Service code is as below
private SomePojoObject uploadFile(MultipartFile mFile, String metaData) {
File file = convertFile(mFile);
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("FILE", new FileSystemResource(file));
map.add("metaData", metaData);
// Some more logic of resttemplate to upload document to server
}
public File convertFile(MultipartFile mFile) {
File file = new File(mFile.getOriginalFilename());
try {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(mFile.getBytes());
fileOutputStream.close();
} catch (IOException e) {
e.printstackTrace();
}
return file;
}
Finally found a rather very simple way to achieve above.
There is no need to convert it to file.!!!!
private SomePojoObject uploadFile(MultipartFile mFile, String metaData) {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("FILE", mFile.getResource());
map.add("metaData", metaData);
// Some more logic of resttemplate to upload document to server
}
I have a REST API for uploading files to server written in Java with Spring Boot. I am testing the API through postman. The API is working fine in Linux Environment, but in windows environment it returns 400 Bad Request as response. There is no change in code in both systems, as I am using the same jar file in both environments.
The POST request consist of a file attachment in the form-data and a parameter userName. My API accepts the data in that format only. I have checked the headers in the request in both the machines, and I am just passing a single header Content-Type as application/json.
Could someone guide me, what might be causing the error? I have checked some answers in stackoverflow for what might be the reasons for HTTP 400 other than the endpoint no being existing. Nothing answered my query.
Edit: Adding the code I am using to upload the file.
private static String UPLOADED_FOLDER_BIRTHDAY = "D:/uploads/birthday/";
#PostMapping("/api/upload/birthday")
public ResponseEntity<?> uploadFileForBirthday(#RequestParam("file") MultipartFile uploadfile, #RequestParam("userName") String userName) {
if (uploadfile.isEmpty()) {
return new ResponseEntity("please select a file!", HttpStatus.OK);
}
try {
saveUploadedFiles(Arrays.asList(uploadfile), UPLOADED_FOLDER_BIRTHDAY);
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity("Successfully uploaded - " +
uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);
}
private void saveUploadedFiles(List<MultipartFile> files, String uploadPath) throws IOException {
for (MultipartFile file : files) {
if (file.isEmpty()) {
continue;
}
byte[] bytes = file.getBytes();
Path path = Paths.get(uploadPath + file.getOriginalFilename());
Files.write(path, bytes);
}
}
I am trying to make a restful controller to upload files. I have seen this
and made this controller:
#RestController
public class MaterialController {
#RequestMapping(value="/upload", method= RequestMethod.POST)
public String handleFileUpload(
#RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
and then i used postman to send a pdf:
But the server crashes with the error:
.MultipartException: Current request is not a multipart request
Again i have found this, and added a bean.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
Unfortunately, it still complains with the same error.
When you are using Postman for multipart request then don't specify a custom Content-Type in Header. So your Header tab in Postman should be empty. Postman will determine form-data boundary. In Body tab of Postman you should select form-data and select file type. You can find related discussion at https://github.com/postmanlabs/postman-app-support/issues/576
It looks like the problem is request to server is not a multi-part request. Basically you need to modify your client-side form. For example:
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
Hope this helps.
I was also facing the same issue with Postman for multipart. I fixed it by doing the following steps:
Do not select Content-Type in the Headers section.
In Body tab of Postman you should select form-data and select file type.
It worked for me.
That happened once to me: I had a perfectly working Postman configuration, but then, without changing anything, even though I didn't inform the Content-Type manually on Postman, it stopped working; following the answers to this question, I tried both disabling the header and letting Postman add it automatically, but neither options worked.
I ended up solving it by going to the Body tab, change the param type from File to Text, then back to File and then re-selecting the file to send; somehow, this made it work again. Smells like a Postman bug, in that specific case, maybe?
In application.properties, please add this:
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
and in your html form, you need an : enctype="multipart/form-data".
For example:
<form method="POST" enctype="multipart/form-data" action="/">
Hope this help!
Check the file which you have selected in the request.
For me i was getting the error because the file was not present in the system, as i have imported the request from some other machine.
In my case, I removed:
'Content-Type': 'application/json',
from my Interceptor, and everything works.
intercept(httpRequest: HttpRequest<any>, httpHandler: HttpHandler): Observable<HttpEvent<any>> {
if (this.authService.isUserLoggedIn() && httpRequest.url.indexOf('login') === -1) {
const authReq = httpRequest.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: this.authService.getBasicAuth()
})
});
return httpHandler.handle(authReq);
} else {
return httpHandler.handle(httpRequest);
}}
You need to add consumes = {MULTIPART_FORM_DATA_VALUE} to your mapping. Full example :
#PostMapping(path = "/{idDocument}/attachments", consumes = {MULTIPART_FORM_DATA_VALUE})
ResponseEntity<String> addAttachmentsToDocumentForm(#PathVariable Long idDocument, #RequestParam("file") MultipartFile[] files){
documentService.addAttachments(idDocument, files);
return ok("your response");
}
#PostMapping("/upload")
public ResponseEntity<ResponseMessage> uploadFile(#RequestParam("file") MultipartFile uploadFile) {
String message = "";
try {
service.save(uploadFile);
message = "Uploaded the file successfully: " + uploadFile.getOriginalFilename();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
} catch (Exception e) {
message = "Could not upload the file: " + uploadFile.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
}
}
in ARC (advanced rest client) - specify as below to make it work
Content-Type multipart/form-data (this is header name and header value)
this allows you to add form data as key and values
you can specify you field name now as per your REST specification and select your file to upload from file selector.
i was facing the same issue with misspelled enctype="multipart/form-data", i was fix this exception by doing correct spelling .
Current request is not a multipart request client side error so please check your form.
i was facing this issue but it was due to the file i was uploading in postman wasn't in the postman working directory
Go to Settings -> General and scroll down to location to find it's location folder and put your files to upload there.
See : Setup Potman working directory
add required=false with the parameter
#RequestParam(required = false,value = "file") MultipartFile file
I am developing an Angular 2 app that requires to serve a file by making a request for a file at the backend.
I am returning the requested file's File object from the backend. But the Jackson is serializing that response automatically, and I am getting file name as a response rather than a file as a whole.
Moreover the file is an XML file. Could anyone help as to how to prevent Jackson from serializing the Java File object that my controller method is sending?
EDIT
Added the controller code that does file serving
#RequestMapping(value = FatcaConstants.GET_XML_FILE, method = RequestMethod.GET)
public File getXML(#RequestParam(value = "filePath") String filePath) {
LOG.info("Getting the XML report from the file path in the Controller");
try {
File file = new File(filePath);
return file;
} catch (Exception e) {
LOG.info("Error in retrieving file " + filePath + " from the FileController");
LOG.info(e.getMessage());
return null;
}
}
I have to send file from Server (from its file system) to Cliente (another pc and store file in a particularly folder) through java code and Rest web service. These files can be even 120MB.
I used MultipartFile to upload file from my web page but I don't know how to download from the client.
It would be good the possibility to use a REST web service that returns both file and message with result of method (true or false if there was an error).
Do you have an idea?
At the moment I use this code in server:
and the best way would be
#Override
#RequestMapping(value = "/oldmethod", method = RequestMethod.GET)
public #ResponseBody Response getAcquisition(#RequestParam(value="path", defaultValue="/home") String path){
File file;
try {
file = matlabClientServices.getFile(path);
if (file.exists()){
InputStream inputStream = new FileInputStream(path);
byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);
return new Response(true, true, out, null);
}
else
return new Response(false, false, "File doesn't exist!", null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "Error during file retrieving!", errorResponse);
}
}
but to the client the code below doesn't work:
public Response getFileTest(#RequestParam(value="path", defaultValue="/home") String path){
RestTemplate restTemplate = new RestTemplate();
Response response = restTemplate.getForObject("http://localhost:8086/ATS/client/file/oldmethod/?path={path}", Response.class, path);
if (response.isStatus() && response.isSuccess()){
try {
Files.write(Paths.get("PROVIAMOCI.txt"),org.apache.commons.io.IOUtils.toByteArray(response.getResult().toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return response;
}
it writes the byte[] characters and not the original text
As per my understanding FileSystemResource used for get the file system using file system URI but it is not over the HTTP. Using FileSystemResource you can access files from your local machine to local accessible file system and from server to your server accesiable file system. But could not access from local to server file system.