跪求android单线程下载报错解决办法
报错如下
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at com.gaoyunfeng.uplooking.www.server.AndroidServer.main
- Java code
package com.download;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;public class DownloadFileActivity extends Activity { public ProgressBar pb; Button btn; int progress = 0; MyAnysTask myAnysTask; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pb = (ProgressBar) findViewById(R.id.progressBar1); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub myAnysTask = new MyAnysTask(); myAnysTask.execute("a1.mp3"); btn.setEnabled(false); } }); } @Override protected void onDestroy() { finish(); // TODO Auto-generated method stub super.onDestroy(); } class MyAnysTask extends AsyncTask<String, Integer, String> { @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub progress += values[0]; pb.setProgress(progress); super.onProgressUpdate(values); } @Override protected String doInBackground(String... params) { Socket socket = null; InputStream inputStream = null ; OutputStream outputStream = null ; OutputStream fileout = null; try { socket = new Socket("192.168.1.100", 9999); byte[] buffer = new byte[8 * 1024]; inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); outputStream.write(params[0].getBytes()); // 设置pb的max inputStream.read(buffer); System.out.println("buffer"+new String(buffer).trim()); if (new String(buffer).trim() != null) { int filecinmax = Integer.parseInt(new String(buffer).trim()); pb.setMax(filecinmax); } outputStream.write("hello".getBytes()); // 开始下载文件 File file = new File("/sdcard/a1.mp3"); if (file.exists()) { file.delete(); } file.createNewFile(); int count = 0; fileout = new FileOutputStream(file); do { count = inputStream.read(buffer); fileout.write(buffer, 0, count); publishProgress(8); } while (count == buffer.length); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { fileout.close(); inputStream.close(); outputStream.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }}
下面是server端
- Java code
package Server;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class Serv { public static void main(String[] args) { ServerSocket ss = null; InputStream in = null ; Socket client = null; InputStream filein = null ; OutputStream outputStream = null; try { byte[] buffer = new byte[1024 * 8]; ss = new ServerSocket(9999); client = ss.accept(); // 首先读取要下载的文件名 in = client.getInputStream(); outputStream = client.getOutputStream(); in.read(buffer); String filename = "D:\\music\\" + new String(buffer).trim(); File file = new File(filename); if (file.exists()) { // 读取文件大小传递给客户端 int count = 0; count = (int) (file.length() / 1024); outputStream.write(("" + count).getBytes()); // 开始向客户端发送文件 前确定连接 buffer = new byte[8 * 1024]; in.read(buffer); if (new String(buffer).trim().equals("hello")) { filein = new FileInputStream(file); do { count = filein.read(buffer); System.out.println(count + ""); outputStream.write(buffer, 0, count); } while (count == buffer.length); System.out.println("发送完成"); filein.close(); } } in.close(); outputStream.close(); client.close(); ss.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); outputStream.close(); client.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}[解决办法]
貌似是在服务器端还没有写完的情况下,客户端关闭了链接,导致服务器端write报错
[解决办法]
问题在 while (count == buffer.length); 这个判断语句上
在 java IO 里 inputStream.read(buffer) 这样的代码是不保证一定会填满字节数组的,特别是在网络连接上,更是非常的不可靠。
[解决办法]
流换一下,用DataOutputStream dos = new DataOutputStream(socket.getOutputStream);
输入流也一样,客户端服务端都一样,一般没有直接用SocketOutputStream来读写数据的吧,至少我没用过
[解决办法]
在write之后flush()一下