[Java] 纯文本查看 复制代码
package com.example.demo.util;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
public class HttpClientUtil {
public static String httpClientUploadFile(String url,MultipartFile file) {
// String remote_url = url;// 第三方服务器请求地址
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
String fileName = file.getOriginalFilename();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
builder.addTextBody("type", "2");// 类似浏览器表单提交,对应input的name和value
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
[Java] 纯文本查看 复制代码
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Configuration
public class ConfigBean {
@Bean
public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setDefaultEncoding("UTF-8");
multipartResolver.setMaxUploadSize(104857600);
multipartResolver.setMaxInMemorySize(40960);
return multipartResolver;
}
}
[Java] 纯文本查看 复制代码
@RequestMapping(value = "/uploadFile/upload", method = RequestMethod.POST)
@ResponseBody
public String upload(@RequestParam(value = "file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}
String filename = UUID.randomUUID().toString();
String suffix = "";
String originalFilename = file.getOriginalFilename();
// 截取文件的后缀名
if (originalFilename.contains(".")) {
suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
}
String fileName = filename + suffix;
String filePath = "D:\\A\\B\\D\\";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
return "上传成功";
} catch (IOException e) {
}
return "上传失败!";
}
@RequestMapping(value = "/uploadFile/uploadPermanent", method = RequestMethod.POST)
@ResponseBody
public String uploadPermanent(@RequestParam(value = "file") MultipartFile file) {
String url = "http://192.168.1.56:9090";
String ss= HttpClientUtil.httpClientUploadFile(url+"/uploadFile/upload",file);
return ss;
}