import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
public class fileup {
String fileName = "D:/test/1.jpg";//文件地址
String fileurl ="http://127.0.0.1:8080/file-server/files/upload";//文件服务器地址
public static void uploadFile(String fileName,String fileurl) {
try {
// 换行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "========7d4a6d158c9";
// 服务器的域名
URL url = new URL(fileurl);
System.out.println("================连接==================");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("================连接成功==================");
// 设置为POST情
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求头参数
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
System.out.println("================上传附件==================");
// 上传文件
File file = new File(fileName);
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件参数,photo参数名可以随意修改
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + fileName
+ "\"" + newLine);
sb.append("Content-Type:application/octet-stream");
// 参数头设置完以后需要两个换行,然后才是参数内容
sb.append(newLine);
sb.append(newLine);
// 将参数头的数据写入到输出流中
out.write(sb.toString().getBytes());
// 数据输入流,用于读取文件数据
DataInputStream in = new DataInputStream(new FileInputStream(
file));
byte[] bufferOut = new byte[1024];
int bytes = 0;
// 每次读1KB数据,并且将文件数据写入到输出流中
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
// 最后添加换行
out.write(newLine.getBytes());
in.close();
// 定义最后数据分隔线,即--加上BOUNDARY再加上--
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
.getBytes();
// 写上结尾标识
out.write(end_data);
out.flush();
out.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
System.out.println("服务器接收数据成功!");
} else {
System.out.println(responseCode+"服务器接收数据失败!");
}
InputStream inout = conn.getInputStream();
ByteArrayOutputStream infoStream = new ByteArrayOutputStream();
int bytesout = 0;
byte[] bufferout = new byte[1024];
while ((bytesout = inout.read(bufferout)) != -1) {
infoStream.write(bufferout, 0, bytesout);
}
inout.close();
infoStream.flush();
infoStream.close();
//获取返回数据进行处理
String json = infoStream.toString("utf-8");
System.out.println(json);
if(json!=null){
JSONObject jsonObj = JSONObject.parseObject(json);
System.out.println(jsonObj.getString("DATA"));
String str = jsonObj.getString("DATA");
List<String> list = JSONObject.parseArray(str,String.class);
for (String strr: list) {
System.out.println(strr);
}
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
}
}