Issue
I have developed a rest client using spring mvc which upload files and form data to a rest service using Jersey.
I can able to see the files that i uploaded, in rest client's Tomcat home directory.
How can i automatically delete the file that is stored in my tomcat, after i get a success response.
Some of my configuration for your reference,
Multipart config in "web.xml"
<multipart-config>
<location>/tmp</location>
<max-file-size>26214400</max-file-size>
<max-request-size>31457280</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
multipart config in "dispatcher-servlet.xml"
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
My business logic,
public Map<Object, Object> upload(ModelMap model) {
Map<Object, Object> responseMap = new HashMap<>();
sendMailBean = (SendMailBean) model.get("sendMailBean");
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
formDataMultiPart.field("firstName", sendMailBean.getFirstname());
formDataMultiPart.field("lastName", sendMailBean.getLastname());
formDataMultiPart.field("fromAddress", sendMailBean.getEmail());
formDataMultiPart.field("subject", sendMailBean.getSubject());
formDataMultiPart.field("text", sendMailBean.getMessage());
List<MultipartFile> files = sendMailBean.getAttachments();
try {
for(MultipartFile file : files) {
File convFile = convert(file);
FileDataBodyPart filePart = new FileDataBodyPart("files", convFile);
filePart.setContentDisposition(FormDataContentDisposition.name("files").fileName(file.getOriginalFilename()).build());
formDataMultiPart.bodyPart(filePart);
}
Client client = new Client();
WebResource webResource = client.resource(----rest url-----);
ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, formDataMultiPart);
if (response.getStatus() != 200) {
model.addAttribute("errormsg", "Failed : HTTP error code : " + response.getStatus());
responseMap.put("model", model);
responseMap.put("redirectToPage", "redirect:/views/error");
} else {
// responseMap.put("redirectToPage", "/views/email");
responseMap.put("model", model);
responseMap.put("redirectToPage", "");
}
}catch (Exception e) {
e.printStackTrace();
}
return responseMap;
}
public File convert(MultipartFile file)
{
File convFile = new File(file.getOriginalFilename());
try {
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return convFile;
}
Solution
Looks like the problem lies with the fileoutstream logic to convert multipart file to file. Below is the workaround that i used to resolve this,
I replaced the conversion logic to,
public File multipartToFile(MultipartFile file) throws IllegalStateException, IOException
{
File tmpFile = new File(System.getProperty("user.dir") + File.separator + file.getOriginalFilename());
file.transferTo(tmpFile);
return tmpFile;
}
and for each multipart file iteration, i have put the converted file to a list and after I finished uploading the file, I iterated my list and deleted the file.
Cheers.
Answered By - Harish ThulasiRam