日期:2014-05-20 浏览次数:20912 次
public void downloadFile(String groupName, String filePath, String fileName) { try { Socket socket = new Socket(serverIP, filePort); InputStream is = socket.getInputStream(); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(new Integer(ClientToServerMessageType.DownloadFile)); oos.writeObject(new String(account)); oos.writeObject(new String(groupName)); oos.writeObject(new String(fileName)); oos.writeObject(new Long(0)); oos.flush(); File file = new File(filePath + fileName); FileOutputStream dos = new FileOutputStream(file); int temp; do { temp = ois.read(); dos.write(temp); //问题就在这,我发现卡在这个循环中,不能跳出,我认为服务器端将-1写进去了的,应该能跳出 }while(temp != -1); dos.flush(); oos.close(); dos.close(); is.close(); JOptionPane.showMessageDialog(null, "文件下载成功"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
//前面的那些account,groupName等已经从流中无误地读出来了,这是向流中写文件的数据 private void provideFile(String groupName, String fileName, ObjectOutputStream oos) { File file = new File(ServerFileSavePath.fileSavePath + "\\" + groupName + "\\" + fileName); FileInputStream dis; try { dis = new FileInputStream(file); int temp; do { temp = dis.read(); oos.write(temp); }while(temp != -1); oos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
do { temp = ois.read(); if(temp ==-1) { break; } dos.write(temp); }while(true);
------解决方案--------------------
你这样会把-1写进去,你要不换成这样试试
dis = new FileInputStream(file);
int temp =0;
while((temp=dis.read())!=-1){
oos.write(temp);
oos.flush();
}
------解决方案--------------------
LZ 的 dis没关闭
private void provideFile(String groupName, String fileName, ObjectOutputStream oos) {
File file = new File(ServerFileSavePath.fileSavePath + "\\" + groupName + "\\" + fileName);
FileInputStream dis
------解决方案--------------------
temp = dis.read();
oos.write(temp);
这两句有问题,temp=-1的时候,write(-1),在客户端读到的应该是255。
所以不正确。
------解决方案--------------------