System.out.println("Please write the right address!");
return;
}
File file = new File(args[0]);
if(!(file.exists() && file.isFile())){
System.out.println("File is not exists!");
return;
}
if(!file.getName().endsWith(".jpg")){
System.out.println("File is not a picture!");
return;
}
if(file.length()>=1024*1024*5){
System.out.println("File is too large!");
return;
}
try {
s = new Socket("192.168.56.1",10001);
} catch (Exception e1) {
throw new RuntimeException("Client create error!");
}
try {
bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
while((len = bis.read(buf)) != -1){
bos.write(buf,0,len);
}
System.out.println("ERROR!!!!!!!");
s.shutdownOutput();
BufferedReader ir = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(ir.readLine());
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bis != null){
try {
bis.close();
} catch (IOException e) {
throw new RuntimeException("BufferedInputStream close error!");
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
throw new RuntimeException("ClientSocket close error!");
}
}
}
}
}
复制代码
服务器端:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
class PicThread implements Runnable{
private Socket s;
PicThread(Socket s){
this.s = s;
}
public void run() {
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
int count = 1;
File file = new File("f:\\"+ip+"\\"+count+".jpg");
while(file.exists())
file = new File("f:\\"+ip+"\\"+(count++)+".jpg");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
BufferedInputStream bis =new BufferedInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
while((len = bis.read(buf)) != -1){
bos.write(buf,0,len);
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("Upload Success");
bw.flush();
} catch (Exception e) {
throw new RuntimeException(ip+"File create error!");
}if(bos != null){
try {
bos.close();
} catch (IOException e) {
throw new RuntimeException(ip+"BufferedOutputStream close error!");
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
throw new RuntimeException(ip+"ServerSocket close error!");
}
}
}
}
public class UploadServer {
public static void main(String[] args) {
while(true){
Socket s = null;
try {
ServerSocket ss = new ServerSocket(10001);
s= ss.accept();
new Thread(new PicThread(s)).start();
} catch (IOException e) {
throw new RuntimeException("Server create error!");
}
}
}
}
复制代码
错误提示为:
F:\Net>javac UploadClient.java
F:\Net>java UploadClient e:pic.jpg
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 java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at UploadClient.main(UploadClient.java:48)