wlt文件 java_springboot 文件上传及java使用post请求模拟文件上传

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值