qq_26720347
qq_26720347
2015-03-26 05:43

java从sftp上下载到本地磁盘的zip文件读取不了,请大家帮忙解答下,谢谢!

10
  • sftp下载zip文件

从sftp上下载到本地的zip文件是没问题的,用压缩工具打开能查看里面的文件,为什么就是读取不了呢?
java从sftp下载zip文件到本地磁盘代码:
import java.io.InputStream;
import java.util.Date;

import com.ibm.gbs.ai.portal.framework.util.DateUtils;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class FtpZipOption {
public static String ftpServer = "***";//有关机密不便给大家看,理解万岁!
public static int ftpPort = 22;
public static String ftpUserName = "***";
public static String ftpPassword = "***";
@SuppressWarnings({ "rawtypes", "resource" })
public static void main(String[] args) throws Exception {
Session session = null;
Channel channel = null;
JSch jsch = new JSch();
if(ftpPort <=0){
//连接服务器,采用默认端口
session = jsch.getSession(ftpUserName, ftpServer);
}else{
//采用指定的端口连接服务器
session = jsch.getSession(ftpUserName, ftpServer ,ftpPort);
}
//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
//设置登陆主机的密码
session.setPassword(ftpPassword);//设置密码

java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//设置登陆超时时间

session.connect();
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd("download/"+DateUtils.convert(DateUtils.convert(DateUtils.addDay(new Date(), -1)), "yyyyMMdd"));
String[] tt = sftp.ls("*.zip").get(0).toString().split(" ");
InputStream input = sftp.get(tt[tt.length-1]);
sftp.get("D:\hh", tt[tt.length-1]);
// ftp.openConnect();
/*if (ftp.createFile("/alipay/download/"+DateUtils.convert(DateUtils.convert(DateUtils.addDay(new Date(), -1)), "yyyyMMdd"))) {
ftp.uploadFile("/alipay/download/"+DateUtils.convert(DateUtils.convert(DateUtils.addDay(new Date(), -1)), "yyyyMMdd"),
"alipay_record_"+DateUtils.convert(DateUtils.convert(DateUtils.addDay(new Date(), -1)), "yyyyMMdd")+".zip",input);
}*/
input.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
session.disconnect();
channel.disconnect();
}
}
}

java读取从sftp上下载到本地的zip文件代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.commons.collections.CollectionUtils;

import com.ibm.gbs.ai.portal.framework.util.StringUtils;

public class testUtils {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws IOException {
// logger.info("进入下载。。。。");
String localFtp ="alipay_record_20150324.txt";
// FtpUtils ftp = new FtpUtils(Const.FTP_SERVER, Const.FTP_PROT, Const.FTP_USER_NAME, Const.FTP_PASSWORD);
// ftp.openConnect();
String productName = "商品名称";
String likeFileName = "财务明细.csv";
ZipFile zipFile = null;
byte[] buffer = new byte[1024];
// InputStream in = ftp.returnFileStream(new String(remoteFile, "gb2312"));
InputStream in = new BufferedInputStream(new FileInputStream("D:\hh\2088001502510167_20150325.zip"));
// CheckedInputStream csumi = new CheckedInputStream(in, new Adler32());
ZipInputStream zin = new ZipInputStream(in);
if (in != null) {
try {
ZipEntry zz = null;
List list = new ArrayList();
if (zin == null) {
return;
}else {
while ((zz = zin.getNextEntry()) != null) {//在这把报错了,错误是:“java.lang.IllegalArgumentException”,zin.getNextEntry()因为ZipInputStream对象zin中根本这个属性
if (zz.isDirectory()) {
return;
}else {
String name = zz.getName();
String [] names = name.split("_");
if (StringUtils.equals(names[names.length-1].trim(), likeFileName)) {
@SuppressWarnings("null")
final BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zz),"gb2312"));
String line;
while ((line = br.readLine()) != null) {
final StringTokenizer st = new StringTokenizer(line, "");
String [] temps = line.split(",");
if (temps.length < 9 || StringUtils.equals(productName, temps[8].trim())) {
continue;
}
final String rowInfo = st.nextToken();
list.add(rowInfo+"\n");
}

                    }
                }
            } 
            if (CollectionUtils.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    buffer = list.get(i).getBytes();
                }
                ByteArrayInputStream input = new ByteArrayInputStream(buffer);

// String filePath = "/alipay/download/"+DateUtils.convert(DateUtils.convert(DateUtils.addDay(new Date(), -2)), "yyyyMMdd");
// ftp.uploadFile(filePath, localFtp, input);
input.close();
}

          }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
               if (in != null) {
                   in.close();
               }
               if (zin != null) {
                   zin.close();
               }
           } catch (Exception e2) {

// logger.error("关闭流异常:"+e2.getMessage());
e2.printStackTrace();
}
}
}

// ftp.close();
// logger.info("下载结束!");

}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答