0

In my spring application, I am trying to create a new file to the temp directory using

File tmpFile = new File(System.getProperty("user.dir") + File.separator + file.getOriginalFilename());

The file is properly created in the Tomcat environment under the temp directory.

But, deploying the same to the websphere, the path which is the java is trying to write to is

C:\Program Files\IBM\WebSphere\AppServer_1\profiles\AppSrv01\temp\WIN7VSNode04\server1\application\application.war\C:\Program Files\IBM\WebSphere\AppServer_1\profiles\AppSrv01\file to be attached.extension

which throws the file not found exception (The filename, directory name, or volume label syntax is incorrect.)

Is there some java settings needs to be changed to the websphere environment? what may be the reason causing this?

Thanks in advance.

Cheers.

  • It looks like the difference is in whatever is happening in file.getOriginalFilename() - that's not a Java API method, and it looks like it might be appending the java working directory an extra time somehow. If you could share the logic of that method, it might be useful. – Jarid Nov 28 '17 at 13:24
  • file.getOriginalFilename() yes it is not a java API, it is a method in spring which gives the filename with the extension " String org.springframework.web.multipart.MultipartFile.getOriginalFilename()" – Harish ThulasiRam Nov 28 '17 at 13:29
  • Your problem is that you do concatenate the "user.dir" directory (C:\Program Files\...) with File.separator and the whole file name (C:\Program Files\IBM\...) which alltogether does not give correct file name. – Michal Nov 28 '17 at 13:43
  • @Michal The getOriginalFileName() provide only the name of the file with its extension and not its entire path – Harish ThulasiRam Nov 28 '17 at 13:51
  • @Harish: You mean alone the value of user.dir contains C:\Program Files\IBM\WebSphere\AppServer_1\profiles\AppSrv01\temp\WIN7VSNode04\server1\application\application.war\C:\Program Files\IBM\WebSphere\AppServer_1\profiles\AppSrv01\f – Michal Nov 28 '17 at 13:57
  • @Michal When i try to print "System.getProperty("user.dir") + File.separator + file.getOriginalFilename()" alone it gives me the path "\C:\Program Files\IBM\WebSphere\AppServer_1\profiles\AppSrv01\attached_file_name.extension", but when used with new File().. it gives the above incorrect path :( – Harish ThulasiRam Nov 28 '17 at 15:00
  • Please build the wanted file name outside (as you mention), escape all \ characters by another \ and try calling the new File. – Michal Nov 28 '17 at 15:08
  • @michal a code sample snippet for the above comment will do ;) – Harish ThulasiRam Nov 28 '17 at 15:11
  • String fileName = System.getProperty("user.dir") + File.separator + file.getOriginalFilename(); String escapedFileName = filename.replace("\\", "\\\\"); new File(escapedFileName); – Michal Nov 28 '17 at 15:47

1 Answers1

1

In your question you state new file to the temp directory but in your code you actually use the system property user.dir which contains the user directory. To write file in the temporary directory please use File#createTempFile or the system property java.io.tmpdir.

Please also use only the file name part from the value returned by file.getOriginalFilename(). You can retrieve the file name for example by Path#get and Path#getFileName

Michal
  • 2,353
  • 1
  • 15
  • 18
  • java.io.tmpdir give me the java temp path from the system variables something like c:/java/temp... which is not needed for my case, and createTempFile() creates a dummy temporary file with the prefix and suffix name provided..... – Harish ThulasiRam Nov 28 '17 at 15:05