0

i need to get the full address of a file which needs to be uploaded by the user using browse button. i tried getAbsolutePath, getAbsoluteFile, getCanonicalPath but they all are giving tomcat/bin location. i need the full path of the file which is to be uploaded.

MultipartFile doc_file = studentInfoBean.getUploadedDocument();

String fileName = doc_file.getOriginalFilename();
String fileExtension = FilenameUtils.getExtension(fileName);
File file = new File(fileName);
File path = file.getAbsoluteFile();
//String path = path.toString()

thank you

Michelle
  • 2,830
  • 26
  • 33
v0ld3m0rt
  • 866
  • 3
  • 12
  • 47
  • Do you mean a user has uploaded a file to your web application, and then on your server you want the original full path of the file from the user's filesystem? (E.g. if I uploaded a file, you'd want the string "C:\Users\Michelle\Documents\file.txt"?) – Michelle Aug 02 '13 at 12:21
  • yes,, when the user uploads a file than i need to convert the file from .xls/.xlsx to .csv. so i need the address/path,, – v0ld3m0rt Aug 03 '13 at 13:50
  • 1
    You can't modify the file directly on the client's filesystem or even read the full path, both for security reasons. You need to save the uploaded file locally on your server, do whatever conversion you need to do, and then send it back to the user (if you're sending it back, otherwise store it where you need to store it). – Michelle Aug 03 '13 at 15:39

1 Answers1

3

You'll probably want to use MultipartFile.transferTo(File dest) to save the uploaded file locally. You can then do your conversion, and whatever you need to do with your .csv file (store it somewhere, send it back to the client, etc.) So the full code might be:

MultipartFile doc_file = studentInfoBean.getUploadedDocument();
File temp_file = new File(doc_file.getOriginalFilename());
doc_file.transferTo(temp_file);
//convert doc_file to .csv
//store locally permanently or return to client
Michelle
  • 2,830
  • 26
  • 33
  • this is really a good trick,, copy that file to the tomcat server using transferTo method and do whatever you want to that file and delete it after you're finished.. – v0ld3m0rt Aug 09 '13 at 04:06