java ftp批量下载异常 5
FTPFile[]ft=ftp.listFiles();for(inti=0;i<local_need.size();i++){file=local_need.get(i...
FTPFile[] ft = ftp.listFiles();
for (int i = 0; i < local_need.size(); i++) {
file = local_need.get(i);
for (int j = 0; j < ft.length; j++) {
ftpfile = ft[j];
if (ftpfile.getName().equals(file)) {
long remotesize = ftpfile.getSize();
File localFile = new File(localPath + "/" + file);
if (localFile.exists()) {
if(localFile.canExecute()){
localFile.delete();
}
System.out.println("Delete success");
FileOutputStream is = new FileOutputStream(
localFile, true);
FileInputStream size = new FileInputStream(
localFile);
long loaclsize = size.available();
//ftp.setRestartOffset(loaclsize);
InputStream in = ftp
.retrieveFileStream(new String(ftpfile
.getName().getBytes("GBK"),
"ISO-8859-1"));
long process = loaclsize / remotesize;
byte[] bytes = new byte[1024];
int c;
while (( c=in.read(bytes,0,1024)) > -1) {
is.write(bytes, 0, c);
is.flush();
loaclsize += c;
long nowProcess = loaclsize / remotesize
* 100;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0) {
this.progressBar
.setValue((int) nowProcess);
}
}
c = in.read(bytes);
}
bytes=null;
is.close();
in.close();
size.close();
System.gc();
this.progressBar.setValue(0);
System.out.println((new Date()) + "-->" + file
+ "-->" + loaclsize + " bytes"
+ " Download Successfully");
Thread.sleep(2000);
}
批量从FTP服务器上下载符合条件的文件,第一个文件下载成功,但是到了第二个文件就在while (( c=in.read(bytes,0,1024)) > -1) 这句报错了:Exception in thread "Thread-3" java.lang.NullPointerException
请问问题是出在哪里呢? 展开
for (int i = 0; i < local_need.size(); i++) {
file = local_need.get(i);
for (int j = 0; j < ft.length; j++) {
ftpfile = ft[j];
if (ftpfile.getName().equals(file)) {
long remotesize = ftpfile.getSize();
File localFile = new File(localPath + "/" + file);
if (localFile.exists()) {
if(localFile.canExecute()){
localFile.delete();
}
System.out.println("Delete success");
FileOutputStream is = new FileOutputStream(
localFile, true);
FileInputStream size = new FileInputStream(
localFile);
long loaclsize = size.available();
//ftp.setRestartOffset(loaclsize);
InputStream in = ftp
.retrieveFileStream(new String(ftpfile
.getName().getBytes("GBK"),
"ISO-8859-1"));
long process = loaclsize / remotesize;
byte[] bytes = new byte[1024];
int c;
while (( c=in.read(bytes,0,1024)) > -1) {
is.write(bytes, 0, c);
is.flush();
loaclsize += c;
long nowProcess = loaclsize / remotesize
* 100;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0) {
this.progressBar
.setValue((int) nowProcess);
}
}
c = in.read(bytes);
}
bytes=null;
is.close();
in.close();
size.close();
System.gc();
this.progressBar.setValue(0);
System.out.println((new Date()) + "-->" + file
+ "-->" + loaclsize + " bytes"
+ " Download Successfully");
Thread.sleep(2000);
}
批量从FTP服务器上下载符合条件的文件,第一个文件下载成功,但是到了第二个文件就在while (( c=in.read(bytes,0,1024)) > -1) 这句报错了:Exception in thread "Thread-3" java.lang.NullPointerException
请问问题是出在哪里呢? 展开
2个回答
展开全部
Thread-3出现空指针异常。也就是说你在多线程运行过程中第三个线程出现问题。可能溢出之类的。Thread-3是系统在你没有给线程命名的情况下系统自动给你的线程命名
追问
InputStream in = ftp.retrieveFileStream(new String(ftpfile.getName().getBytes("GBK"),"ISO-8859-1"));
我找出在下载第二个文件的时候,这一句in的值为null,问题是出在这里,但是下载第一个就没有问题呢,这是什么原因?
追答
怎么总觉得是你在操作输入输出流是,一些细节处问题了。比如在进行多线程下读取文件的两个线程不能同时进行(怕产生指针地址的冲突)。还有就是IO流的开关控制好。你只给了这么一段,是看不到全局的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询