上传文件
1:controller层
@RequestMapping("/uploadFrImage")
@ResponseBody
public JSONObject uploadFrImage(@RequestParam MultipartFile[] files,String uuid, String security,String industry) {
try {
String urlStr = "localhost:8080/xxx/uploadFrImage";
String url = baseUrl+urlStr;
Map strParams = new HashMap();
strParams.put("uuid",uuid);
strParams.put("security",security);
strParams.put("industry",industry);
Map<String,InputStream> fileParams = new HashMap<>();
for(int i=0;i<files.length;i++){
fileParams.put(files[i].getOriginalFilename(),files[i].getInputStream());
}
String str = fasCommonService.uploadFile(url,strParams,fileParams);
return JSON.parseObject(str);
} catch (Exception e) {
logger.error("startOcr Exception [{}]", e.getMessage());
return null;
}
}
2:service层
//换行
private static final String LINE_END = "\r\n";
// 前缀
private static final String PREFIX = "--";
// 边界标识 随机生成
private static final String BOUNDARY = UUID.randomUUID().toString();
// 编码格式
private static final String CHARSET = "utf-8";
// 数据类型
private static final String CONTENT_TYPE = "multipart/form-data";
// 超时时间
private static final int TIME_OUT = 20 * 1000;
private HttpURLConnection getHttpURLConnection(String actionUrl){
HttpURLConnection httpURLConnection = null;
try {
// 统一资源
URL url = new URL(actionUrl);
// http的连接类
httpURLConnection =(HttpURLConnection) url.openConnection();
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
// 设置是否向httpUrlConnection输出
httpURLConnection.setDoOutput(true);
// Post 请求不能使用缓存
httpURLConnection.setUseCaches(false);
// 设定请求的方法,默认是GET
httpURLConnection.setRequestMethod("POST");
// 读取时间
httpURLConnection.setReadTimeout(TIME_OUT);
// 连接时间
httpURLConnection.setConnectTimeout(TIME_OUT);
// 设置请求头参数
// 设置字符编码连接参数
httpURLConnection.setRequestProperty("Connection