我写了个socket传输压缩文件的代码,接收之后文件总是比发送方生成的大,发送方生成的压缩文件解压没问题,但是接收的文件解压就解不开,总是报错,
请高手指点。代码如下:
client
byte[] exportData = getFjInterfaceService().export(comId, saleOrgId,
crtDate);
File zf = new File("RetailerOrder" + crtDate + ".Zip");
ZipOutputStream zipos;
BufferedInputStream fos = null;
OutputStream doc = null;
Socket s = null;
try {
zipos = new ZipOutputStream(new FileOutputStream(zf));
zipos.setMethod(ZipOutputStream.DEFLATED); // 设置压缩方法 zipos.putNextEntry(new ZipEntry("RetailerOrder" + crtDate
+ ".Order"));// 创建文件
DataOutputStream data_output = new DataOutputStream(
new BufferedOutputStream(zipos));
data_output.write(exportData);
data_output.flush();
data_output.close();
// 读取压缩文件并写入socket流
s = new Socket(ip, Integer.parseInt(port));
fos = new BufferedInputStream(new FileInputStream(zf));
OutputStream netOut = s.getOutputStream();
doc = new BufferedOutputStream(netOut);
// 创建文件读取缓冲区
byte[] head = new byte[4];
String len = String.valueOf(fos.available());
head = len.getBytes();
doc.write(head, 0, 4);
doc.flush();
int p = 0;
byte[] buf = new byte[1024];
while (fos.read(buf) != (-1)) { // 是否读完文件
log.debug(p++ + "--" + buf);
doc.write(buf);// 把文件数据写出网络缓冲区
doc.flush();// 刷新缓冲区把数据写往客户端
}
doc.close();
fos.close();
Message success = new Message("send","发送" +StruObjectFactory.getOrganNameByOrganId(saleOrgId)+ "订单数据成功!");
messageHandler.addMessage("success", success);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (doc != null)
doc.close(); if (fos != null)
fos.close(); if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
server: BufferedInputStream bi = null;
BufferedOutputStream fileOut = null;
ZipInputStream zipis;
try {
bi = new BufferedInputStream(s.getInputStream());
String savePath = "D:\\RetailerOrder200806231520300001.Zip";
File fs = new File(savePath);
fileOut = new BufferedOutputStream( new FileOutputStream(fs));
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
long len = 0;
byte[] head = new byte[4];
bi.read(head, 0, 4);
int i=0;
while (bi.read(buf) != -1) {
 //System.out.println(i+++"--"+buf);
fileOut.write(buf);
fileOut.flush();
}
fileOut.close();
bi.close();
/* // 解压缩,读数据
zipis = new ZipInputStream(new FileInputStream(fs));
ZipEntry ze;// 压缩内部文件入口
// System.out.print("----"+zipis.getNextEntry());
while ((ze = zipis.getNextEntry()) != null) { String file = "D:\\" + ze.getName();// 生成文件绝对路径及文件名
File newFile = new File(file);
System.out.println(file);
newFile.createNewFile();
BufferedReader data_input = new BufferedReader(
new InputStreamReader(zipis));// 源
BufferedWriter fileWriter = new BufferedWriter(
new OutputStreamWriter(// 目标
new FileOutputStream(newFile)));
String str = "";
while ((str = data_input.readLine()) != null) {
fileWriter.write(str);
// System.out.println(i+++"---"+str);
fileWriter.write("\n");
fileWriter.flush();
}
fileWriter.flush();
fileWriter.close();
// data_input.close(); }
zipis.close();*/
} catch (IOException e) {
System.out.println(e.toString());
} finally {
System.out.println("Closing Connection...\n"); try {
if (bi != null)
bi.close(); if (fileOut != null)
fileOut.close(); if (s != null)
s.close();
} catch (IOException e) {
}
}
}

解决方案 »

  1.   

    既然是文件变大,那可能是你的读写时方法有问题。
    Client:
    int p = 0;
    byte[] buf = new byte[1024];
    while (fos.read(buf) != (-1)) { // 是否读完文件
    log.debug(p++ + "--" + buf);
    doc.write(buf);// 把文件数据写出网络缓冲区
    doc.flush();// 刷新缓冲区把数据写往客户端
    }
    改为
    int p = 0;
    byte[] buf = new byte[1024];
            int len;
    while ((len = fos.read(buf)) != (-1)) { // 是否读完文件
    log.debug(p++ + "--" + buf);
    doc.write(buf, 0, len);// 把文件数据写出网络缓冲区
    doc.flush();// 刷新缓冲区把数据写往客户端
    }
      

  2.   

    你Server端也要做类似的修改。你应该知道的。
      

  3.   

    IO中有ZipInputStream等流啊,可以直接用的