packagecom.example.demo;import java.io.*;importjava.net.HttpURLConnection;importjava.net.URL;importjava.nio.charset.Charset;importjava.util.HashMap;importjava.util.Map;public classTestRequest {private static final String host = "http://localhost:8080";private static final String url = host + "/uploadFile";public static voidmain(String[] args) {
String fileName= "C:\\Users\\Admin\\Pictures\\1.PNG";
Map map = new HashMap<>();
map.put("id", 10000);
map.put("name", "Admin");try{
uploadFile(fileName, map);
}catch(Exception e) {
e.printStackTrace();
}
}public static void uploadFile(String fileName, Map map) throwsException {//换行符
final String newLine = "\r\n";final String boundaryPrefix = "--";//定义数据分隔线
String BOUNDARY = "========7d4a6d158c9";//服务器的域名
URL url = newURL(TestRequest.url);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();//设置为POST情
conn.setRequestMethod("POST");//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);//设置请求头参数
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +BOUNDARY);try(
OutputStream outputStream=conn.getOutputStream();
DataOutputStream out= newDataOutputStream(outputStream);
) {//传递参数
if (map != null) {
StringBuilder stringBuilder= newStringBuilder();for (Map.Entryentry : map.entrySet()) {
stringBuilder.append(boundaryPrefix)
.append(BOUNDARY)
.append(newLine)
.append("Content-Disposition: form-data; name=\"")
.append(entry.getKey())
.append("\"").append(newLine).append(newLine)
.append(String.valueOf(entry.getValue()))
.append(newLine);
}
out.write(stringBuilder.toString().getBytes(Charset.forName("UTF-8")));
}//上传文件
{
File file= newFile(fileName);
StringBuilder sb= newStringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"").append(fileName)
.append("\"").append(newLine);
sb.append("Content-Type:application/octet-stream");
sb.append(newLine);
sb.append(newLine);
out.write(sb.toString().getBytes());try(
DataInputStream in= new DataInputStream(newFileInputStream(file));
) {byte[] bufferOut = new byte[1024];int bytes = 0;while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut,0, bytes);
}
out.write(newLine.getBytes());
}catch(Exception e) {
e.printStackTrace();
}
}//定义最后数据分隔线,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix +newLine)
.getBytes();//写上结尾标识
out.write(end_data);
out.flush();
}catch(Exception e) {
e.printStackTrace();
}//定义BufferedReader输入流来读取URL的响应
try(
InputStream inputStream=conn.getInputStream();
InputStreamReader inputStreamReader= newInputStreamReader(inputStream);
BufferedReader reader= newBufferedReader(inputStreamReader);
) {
String line= null;while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}