导言
此篇文章,带来最简单的文件发送,第一种方式不推荐。
指定文件格式:new PrintWriter(new FileWriter())的方法比较低效,不能指定文件编码,为了指定文件编码我们可以:
PrintWriter FileStorageAddress=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(“Target File Address”),“UTF-8”)));
的方式去指定。
**
第一种
服务端:
import java.net.*;
import java.io.*;
public class ServerInfoObtainExec{
public static void main(String[] ServerInfoObtainExec)throws Exception{
ServerSocket CreateServerAddress=new ServerSocket(1030);
Socket ObtainOtherOfInfoPoint=CreateServerAddress.accept();
//开始监听
BufferedReader PortInfoStorageStream=new BufferedReader(new InputStreamReader(ObtainOtherOfInfoPoint.getInputStream()));
//建立文件接收
PrintWriter FileStorageAddress=new PrintWriter(new OutputStreamWriter(new FileOutputStream("Target File Address")),true);
//建立文件行的放置地址
String RemoteInputOfInfo=null;
while((RemoteInputOfInfo=PortInfoStorageStream.readLine())!=null){
if(RemoteInputOfInfo.equals("endLaunch")){break;}
FileStorageAddress.println(RemoteInputOfInfo);
}
//第一时间判断是否被发送端请求停止
PrintWriter ReturnUserOfEndInfo=new PrintWriter(ObtainOtherOfInfoPoint.getOutputStream(),true);
ReturnUserOfEndInfo.println("Launch Of Info Complete Obtain !");
//接受完的信息
ReturnUserOfEndInfo.close();
CreateServerAddress.close();
PortInfoStorageStream.close();
//将资源停止占用并且关闭
}
}
然后时客户端:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ClientLaunchExec{
public static void main(String[] ClientInfoLaunch)throws Exception{
Socket RemoteOrderTargetAddress=new Socket("192.168.0.118",1030);
BufferedReader LoadWantLaunchOfLocalFile=new BufferedReader(new FileReader("Want Launch Of File Address"));
//要发送的文件地址
PrintWriter LaunchFileToServer=new PrintWriter(RemoteOrderTargetAddress.getOutputStream(),true);
//建立发送
String LaunchAreaOfInfo=null;
while((LaunchAreaOfInfo=LoadWantLaunchOfLocalFile.readLine())!=null){
LaunchFileToServer.println(LaunchAreaOfInfo);
}
//发送每一行
LaunchFileToServer.println("endLaunch");
RemoteOrderTargetAddress.shutdownOutput();
//发送完时要求接收端停止
BufferedReader ObtainRemoteOfEndInfo=new BufferedReader(new InputStreamReader(RemoteOrderTargetAddress.getInputStream()));
System.out.println(ObtainRemoteOfEndInfo.readLine());
//返回完结信息
RemoteOrderTargetAddress.close();
LoadWantLaunchOfLocalFile.close();
//将资源停止占用并且关闭
}
}
**
第二种
也是我推荐的字节数组方式,这一种是新手阶段最简单的了
服务端
ServerSocket CreateServerAddress=new ServerSocket(1030);
Socket DataActive=CreateServerAddress.accept();
//如果有人连接了
InputStream Data=DataActive.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] Buffer_DATA = new byte[1024];
int NUM_DATA_LENGTH;
while((NUM_DATA_LENGTH = Data.read(Buffer_DATA)) != -1) {
byteArrayOutputStream.write(Buffer_DATA, 0, NUM_DATA_LENGTH);
}
byteArrayOutputStream.flush();
//保存到本地
FileOutputStream O=new FileOutputStream("路径");
O.write(byteArrayOutputStream.toByteArray());
O.close();
客户端
String Path="本地文件路径";
//读取本地文件
FileInputStream obtainData = new FileInputStream(Path);
byte[] StorageDataHome = new byte[(int)(new File(Path)).length()];
ByteArrayOutputStream RETURN_DATA = new ByteArrayOutputStream();
int StorageByte;
while((StorageByte = obtainData.read(StorageDataHome)) > 0) {
RETURN_DATA.write(StorageDataHome, 0, StorageByte);
RETURN_DATA.flush();
}
obtainData.close();
Socket RemoteOrderTargetAddress=new Socket("192.168.0.118",1030);
RemoteOrderTargetAddress.shutdownOutput();
//获取输出流
OutputStream Obtain=RemoteOrderTargetAddress.getOutputStream();
Obtain.write(RETURN_DATA.toByteArray());
Obtain.close();
第二种的解释
名称 | 作用 |
---|---|
ServerSocket | 服务器构建 |
accept | 当有人进入时就停止阻塞 |
getInputStream | 获取输入流 |
new File(Path)).length() | 只是为了获取字节数推荐->obtainData.available() |
read | 读取 |
write | 写入流 |
flush | 刷新流 |
close | 退出文件,不这样会有作用 |
toByteArray | ByteArrayOutputStream自带的API转化为字节数组 |
getInputStream | 获取输入流 |
第一种的解释
名称 | 作用 |
---|---|
BufferedReader | 字符缓冲流-文中只是进行读取 |
PrintWriter | 对Socket的输出流的封装 |
readLine | 读取一行 |
println | 发送一行字符串到服务器 |
shutdownOutput | 服务器端读取数据不堵塞 |
InputStreamReader | 高效读 |
OutputStreamWriter | 高效写 |
为啥不推荐这种方法
因为在每读取一行就发送一次低效,发送太快很有可能会丢包,导致内容不齐全,发送时间拉长。