-1
  1. String testpath1 = "smb://host_address/File_Folder";
  2. File testFile1 = new File(testpath1 + fi.getName());
  3. fi.write(testFile1);

This code in Java to read and write file in Linux server. However, after creating file(at line 2), file path becomes smb:/host_address/File_Folder/File_Name.

I want the file path as smb://host_address/File_Folder/File_Name.

Please help!

saher
  • 51
  • 2
  • 10
  • What's with the list on the first three lines of your question? Shouldn't it be a code block? – Cas Oct 06 '16 at 15:03
  • Yes, it is code block. It should be easy to read so given the line numbers. – saher Oct 07 '16 at 05:32
  • I don't think people will have problems reading three lines of code without line numbers. Right now it isn't formatted as code, which actually makes it harder to read. Please remove the line numbers and simply indent the code with four spaces to turn it into a code block. Stack overflow will take care of making it readable :) – Cas Oct 07 '16 at 05:39

3 Answers3

0

Java File class converts the pathname to an abstract pathname depending on the environment it is running it. . An abstract pathname has two components:

  1. An optional system dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or"\\" for a Microsoft Windows UNC path
  2. A sequence of zero or more string names

I guess Java might be using the abstract pathname of Unix based system in your case resulting in the lose of one /

You need to use SmbFile class to open a SmbFile SmbFile file = new SmbFile(testpath1 + fi.getName()) You can see SmbFile doc and Java File for more info.

Moonstruck
  • 503
  • 4
  • 10
  • Thank you Jainul! I am using FileItem (fi), its write method requires File object. fi.write(File file). If I use SmbFile then how to convert SmbFile Object to File? – saher Oct 03 '16 at 11:33
  • You can read more of it here. http://stackoverflow.com/a/36473193/6912578 – Moonstruck Oct 03 '16 at 11:41
0

Edit: Incorrect approach.

Four slashes in a row might work. In Java and a few other languages, // is reduced to /, because / is an escape character to insert invisible characters such as tabs and new line breaks. Test ////

See also: What are all the escape characters in Java?

Community
  • 1
  • 1
0

Did not get solution to use '//' in java, however if any developer wants to upload file on Linux server using Java Programming then he/she can use below snippet. Thought might be helpful for developers like me :)

SmbFile originalFile = new SmbFile(filePath + fi.getName());
InputStream in;
in = fi.getInputStream(); //fi is FileItem
SmbFileOutputStream out;
out = new SmbFileOutputStream(originalFile);
IOUtils.copy(in, out);
in.close();
out.close();
saher
  • 51
  • 2
  • 10