在我的程序中,我在文档中使用了an example改编的JSch软件包,它描述了如何将文件从远程服务器复制到本地计算机。虽然该程序似乎可以正常工作,但在复制过程中文件似乎已损坏,并且当我尝试从bash播放它们时,出现一个错误消息:“播放失败格式:无法打开输入文件79_97_729.wav” :WAVE:找不到RIFF标题“。如何在java中将wav文件从一个目录复制到另一个目录?
我的复制方法如下:
public void copyFile(File file, String newName) throws JSchException, IOException{
String prefix = null;
if (new File(destination).isDirectory()){
prefix = destination + File.separator;
}
JSch jsch = new JSch();
Session session = jsch.getSession("username", "network");
session.setUserInfo(new MyUserInfo());
session.connect();
String command = "scp -f " + file.getAbsolutePath();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
byte[] buf = new byte[1024];
// send '\0'
buf[0]=0; out.write(buf, 0, 1); out.flush();
while(true){
int c=checkAck(in);
if(c!='C'){
break;
}
// read '0644 '
in.read(buf, 0, 5);
long filesize=0L;
while(true){
if(in.read(buf, 0, 1)<0){
// error
break;
}
if(buf[0]==' ')break;
filesize=filesize*10L+(long)(buf[0]-'0');
}
// send '\0'
buf[0]=0; out.write(buf, 0, 1); out.flush();
// read a content of lfile
fos=new FileOutputStream(prefix == null ? destination : prefix + newName);
int foo;
while(true){
if(buf.length<filesize) foo=buf.length;
else foo=(int)filesize;
foo=in.read(buf, 0, foo);
if(foo<0){
// error
break;
}
fos.write(buf, 0, foo);
filesize-=foo;
if(filesize==0L) break;
}
fos.close();
fos=null;
// send '\0'
buf[0]=0; out.write(buf, 0, 1); out.flush();
}
session.disconnect();
}
是否有某种适应我可以做控制的事实,我复制文件以WAV格式?任何帮助,将不胜感激!
你只是在同一台机器上从一个目录复制到另一个目录?或者这是通过网络传输的? – Lucas 2012-07-26 20:00:48
@卢卡斯,在我看来,这是通过网络传输。看起来你正在通过做所有这些疯狂的东西来破坏文件。为什么不使用[this](http://www.java2s.com/Code/Java/File-Input-Output/copyCompletelyInputStreaminputOutputStreamoutput.htm) – 2012-07-26 20:05:08
与原始文件相比,结果文件的大小是多少? – jtahlborn 2012-07-26 20:10:43