客户端
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public class MyClient {
public static void main(String[] args) throws Exception {
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String fileName = br1.readLine();
String newFileName = fileName.substring(fileName.lastIndexOf("/") + 1);
File file = new File(fileName);
boolean flag = true;
if (!file.exists() || file.length() > 2 * 1024 * 1024)
flag = false;
String fileExt = fileName.substring(fileName.lastIndexOf("."));
if(!".jpg".equals(fileExt))
flag=false;
if(!flag) {
System.out.println("上传的文件不合法!");
System.exit(1); //直接退出应用,如果0表示正常退出,非0表示非正常退出
}
Socket socket = new Socket("localhost", 9000);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
PrintStream ps = new PrintStream(os);
ps.println("put " + newFileName+","+file.length());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
if ("ok".equalsIgnoreCase(str)) {
InputStream is2 = new FileInputStream(fileName);
byte[] buffer = new byte[8192];
int len = 0;
while ((len = is2.read(buffer)) > 0) {
ps.write(buffer, 0, len);
}
ps.flush();
str=br.readLine();
System.out.println(str);
}
br.close();
ps.close();
socket.close();
}
}
服务端
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
PrintStream ps = new PrintStream(os);
String str = br.readLine();
if (str.startsWith("put ")) {
// ps.println("put " + newFileName+","+file.length());
String fileName = str.substring(str.indexOf(" ") + 1, str.lastIndexOf(","));
String slen = str.substring(str.lastIndexOf(",") + 1);
long length = 0;
try {
length = Long.parseLong(slen.trim());
} catch (Exception e) {
length = -1;
}
if (length <= 0)
throw new RuntimeException("上传失败!");
ps.println("ok");
long size = 0;
fileName=fileName.substring(0,fileName.lastIndexOf("."))+(new Date().getTime())+fileName.substring(fileName.lastIndexOf("."));
OutputStream os2 = new FileOutputStream("photo/" + fileName);
byte[] buffer = new byte[8192];
int len = 0;
while ((len = is.read(buffer)) > 0) {
os2.write(buffer, 0, len);
size += len;
//这里有问题,可以使用子线程解决
if(length<=size)
break;
}
os2.close();
if (length == size) {
ps.println("上传成功!");
} else {
File file = new File("photo/" + fileName);
if(file.exists())
file.delete();
ps.println("上传失败!");
}
}
br.close();
ps.close();
socket.close();
ss.close();
}
}