任意文件上传——tcp

时间:2023-03-10 06:40:28
任意文件上传——tcp
package chaoba;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException; public class ChaobaClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1", 10009); FileInputStream fileName = getFileName(s);
rece(s);
sendFile(s, fileName);
rece(s);
s.close(); } /*
* 获取发送文件名。发送给服务端。。。本地验证,存在安全隐患。可造成任意文件上传
*
*/
public static FileInputStream getFileName(Socket s) {
try {
File file = new File("C:\\pic\\chaoba.txt");
FileInputStream fileIn = new FileInputStream(file);
String name = file.getName();
OutputStream outputStream = s.getOutputStream();
outputStream.write(name.getBytes());
return fileIn; } catch (Exception e) {
// TODO: handle exception
}
return null; } /*
*
* 发送文件方法
*
*
*/
public static void sendFile(Socket s, FileInputStream fileIn) {
try {
byte[] bufFile = new byte[1024];
OutputStream outputStream2 = s.getOutputStream();
int read = 0;
while ((read = fileIn.read(bufFile)) != -1) {
outputStream2.write(bufFile);
outputStream2.flush(); } s.shutdownOutput();
fileIn.close(); } catch (Exception e) {
// TODO: handle exception
}
} /*
* 接收服务端方法
*
*/
public static void rece(Socket s) { try {
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String readLine = bufIn.readLine();
System.out.println(readLine);
} catch (Exception e) {
// TODO: handle exception
}
} }
package chaoba;

import java.awt.image.ReplicateScaleFilter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; public class ChaobaServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10009);
Socket accept = ss.accept();
String receName = receName(accept);
receFile(accept, receName);
sendRece(accept);
accept.close();
ss.close(); }
public static void sendRece(Socket accept) throws IOException {
OutputStream outputStream1 = accept.getOutputStream();
PrintWriter pw1 = new PrintWriter(outputStream1, true);
pw1.println("成功上传");
} /*
* 接收文件名
*
*
*/
public static String receName(Socket accept) { try {
InputStream inputStream = accept.getInputStream();
byte[] buf = new byte[1024]; int read = inputStream.read(buf);
String name = new String(buf, 0, read);
System.out.println(name); OutputStream outputStream = accept.getOutputStream();
PrintWriter pw = new PrintWriter(outputStream, true);
pw.println("成功获取文件名");
return name;
} catch (Exception e) {
// TODO: handle exception
}
return null;
} public static void receFile(Socket accept, String name) { try {
FileOutputStream fileOut = new FileOutputStream(name);
byte[] bufFile = new byte[1024];
InputStream inputStream2 = accept.getInputStream();
int read2 = 0;
while ((read2 = inputStream2.read(bufFile)) != -1) { fileOut.write(bufFile, 0, read2);
fileOut.flush(); } fileOut.close();
} catch (Exception e) {
// TODO: handle exception
}
} }