标红色的代码你看下,你都已经封装成字符流去读取了,下面的字节流当然读取不出来的,这一点上个人认为是这样的Socket输出的时候可以,但是输入的时候就不行了。
蓝色的代码是我加的,你打印看看。
class UploadThread implements Runnable{
private Socket s;
public UploadThread(Socket s){
this.s=s;
}
public void run() {
String ip=s.getInetAddress().getHostAddress();
BufferedInputStream bis=null;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
BufferedReader br=null;
PrintWriter pw=null;
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String filename=br.readLine();
String line = null;
while((line = br.readLine)!= null){
System.out.println(line);
}
fos=new FileOutputStream(new File(ip+filename));
bis=new BufferedInputStream(s.getInputStream());
int len=0;
byte[]buf=new byte[1024];
len=bis.read(buf);
while((len=bis.read(buf))!=-1){
fos.write(buf,0,len);
fos.flush();
System.out.println(new String(buf,0,len ));
}
pw=new PrintWriter(s.getOutputStream(),true);
pw.println("文件上传成功!");
s.close();
}catch(Exception e){
throw new RuntimeException("服务器上传文件失败!");
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException io1){
throw new RuntimeException("服务器读取流关闭失败");
}
if(pw!=null)
pw.close();
try{
if(fos!=null)
fos.close();
}catch(IOException io1){
throw new RuntimeException("服务器文件输出流关闭失败");
}
}
}
} |