0

I have a file upload servlet which works fine.

In the doPost() method I read the javax.servlet.http.Part:

final Part filePart = request.getPart("file");

When I log the filePart.toString() I get the following output:

(java.lang.String) File name=my_file.txt, StoreLocation=C:\my_path\payara41\glassfish\domains\domain1\generated\jsp\my_project\upload__78661e49_17d75560e3a__7ffb_00000030.tmp, size=35659896bytes, isFormField=false, FieldName=file

Now I am interested in StoreLocation, the upload loacation and the generic temp file name for my files in application server.

I wrote therefor the following method which parse the toString() output:

private String getStoreLocationPathname(final Part filePart) {
    final String filePartStr = filePart.toString();
    final int n = filePartStr.indexOf("StoreLocation=") + "StoreLocation=".length();
    final int m = filePartStr.substring(n+1).indexOf(".tmp, ") + ".tmp".length();
    final String storeLocationPathname = filePartStr.substring(n, n+m+1);
    return storeLocationPathname;
}

I think this a hack. There are a better solution to get StoreLocation?

My question is neither about the upload directory nor the original file name nor about the possibility to copy the files to another directory as discussed in Recommended way to save uploaded files in a servlet application. I am interested in the generic file name in the application server like upload__78661e49_17d75560e3a__7ffb_00000030.tmp. This can i see in the StoreLocation of the toString() output.

Thank you for your hints Ann

  • *"I think this a hack"* You are COMPLETELY right! That's explained in abovelinked duplicate (you're basically talking about `@MultipartConfig(location)`, just search for references to that in abovelinked duplicate, e.g. *"Please note that @MultipartConfig(location) does not specify the final upload destination, but the temporary storage location for the case file size exceeds memory storage threshold"*). *"There are a better solution to get StoreLocation?"* Absolutely! That's answered in the abovelinked duplicate. Once implemented that, just use `file.toPath()` to obtain the "store location". – BalusC Dec 03 '21 at 15:52

0 Answers0