我尝试将文件从网络驱动器复制到文件夹,我可以在线访问它们。文件看起来像这样,例如
\\GVSSQLVM\Lieferscheine\20011023\Volumed 5005.00000063.doc
当我在。中输入此地址时,我可以在Windows资源管理器中访问
将文件复制到的目的地是
C:\Program Files\jbossAS\server\default\deploy\ROOT.war\tmp\Volumed 5005.doc
我在使用以下代码复制文件时遇到了麻烦:
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();
}
输入为null
。我读到空格字符可能很麻烦,并按照建议将source
放在FileInputStream
引号中,但是new File
似乎将整个文件放到了
c:\Program Files\jbossAS\bin\'\GVSSQLVM\Lieferscheine\20011023\Volumed 5005.00000063.doc'
它似乎首先写入当前路径,然后添加给定的一个反斜杠少;它仍然没有找到该文件。
file.extists()
产生错误。
感谢您的帮助!
答案 0 :(得分:2)
使用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;
}