Reputation: 363
I'm trying to copy files from a network drive to a folder, where I can access them online. A file looks like this, for example
\\GVSSQLVM\Lieferscheine\20011023\Volumed 5005.00000063.doc
which I can access in the Windows explorer, when I type this address in.
And the destination to copy the file to would be
C:\Program Files\jbossAS\server\default\deploy\ROOT.war\tmp\Volumed 5005.doc
I run into trouble copying the file with following code:
String doc_dir = "\\\\GVSSQLVM\\Lieferscheine\\20011023\\";
String doc_file = doc_dir.concat(doc.getUniqueFileName());
File source = new File(doc_file);
String home_url = System.getProperty("jboss.server.home.url");
String home_dir = home_url.substring(5); // cut out preceding file:/
String tmp_dir = home_dir.concat("deploy/ROOT.war/tmp/");
String dest_file = tmp_dir.concat(title);
File dest = new File(dest_file);
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
input is null
. I read that the space character could be troublesome, and followed the advice to put the source
for the FileInputStream
in quotes, but then new File
seems to screw the whole filename up into
c:\Program Files\jbossAS\bin\'\GVSSQLVM\Lieferscheine\20011023\Volumed 5005.00000063.doc'
where it seems to write the current path first, then add the given one with one backslash less; and it still doesn't find the file.
file.extists()
yields false.
Thank you for your help!
Upvotes: 3
Views: 15042
Reputation: 389
try the following code its using JCIF.jar:
public static boolean createCopyOnNetwork(String domain,String username,String password,String src, String dest) throws Exception
{
//FileInputStream in = null;
SmbFileOutputStream out = null;
BufferedInputStream inBuf = null;
try{
//jcifs.Config.setProperty("jcifs.smb.client.disablePlainTextPasswords","true");
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain,username,password); // replace with actual values
SmbFile file = new SmbFile(dest, authentication); // note the different format
//in = new FileInputStream(src);
inBuf = new BufferedInputStream(new FileInputStream(src));
out = (SmbFileOutputStream)file.getOutputStream();
byte[] buf = new byte[5242880];
int len;
while ((len = inBuf.read(buf)) > 0){
out.write(buf, 0, len);
}
}
catch(Exception ex)
{
throw ex;
}
finally{
try{
if(inBuf!=null)
inBuf.close();
if(out!=null)
out.close();
}
catch(Exception ex)
{}
}
System.out.print("\n File copied to destination");
return true;
}
Upvotes: 3