文件上传原理
文件上传客户端
package com.mujiachao.tcp;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class WenJianShangChuanClient {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:\\电影\\111\\kk.jfif");
Socket socket = new Socket("127.0.0.1",8888);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
while ((len=fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
socket.shutdownOutput();
while ((len=inputStream.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fileInputStream.close();
socket.close();
}
}
文件上传服务器
package com.mujiachao.tcp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class WenJianShangChuanServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
File file = new File("F:\\电影\\111\\upload");
if (!file.exists()){
file.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(file + "\\99.png");
int len=0;
byte[] bytes=new byte[1024];
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
accept.getOutputStream().write("上传成功".getBytes());
fileOutputStream.close();
accept.close();
serverSocket.close();
}
}
文件上传案例阻塞问题 解决方案: socket.shutdownOutput();
文件上传案例优化(循环接收,文件命名,多线程)
package com.mujiachao.tcp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newFixedThreadPool;
public class WenJianShangChuanServerYouHua {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
while (true){
Socket accept = serverSocket.accept();
ExecutorService executorService = newFixedThreadPool(10);
executorService.submit(new Thread(){
@Override
public void run() {
try( InputStream inputStream = accept.getInputStream()) {
File file = new File("F:\\电影\\111\\upload");
if (!file.exists()){
file.mkdirs();
}
String fiLName="\\mu"+System.currentTimeMillis()+ new Random().nextInt(999999)+".png";
FileOutputStream fileOutputStream = new FileOutputStream(file + fiLName);
int len=0;
byte[] bytes=new byte[1024];
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
accept.getOutputStream().write("上传成功".getBytes());
fileOutputStream.close();
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}
package com.mujiachao.tcp;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class WenJianShangChuanClientYouHua {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:\\电影\\111\\kk.jfif");
Socket socket = new Socket("127.0.0.1",8888);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
while ((len=fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
socket.shutdownOutput();
while ((len=inputStream.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fileInputStream.close();
socket.close();
}
}