uploadPicture
1.上传图片需要用到 (
HttpServletRequest httpServletRequest, @RequestParam(required = false) MultipartFile[] file) 添加@RequestParam(required = false),没有强制一定要传图片的意思
String path = "/res/notification/campaign"; //定义一个路径
// 生成图片名
String filename = "campaign_" + RandomStringUtils.randomAlphanumeric(8); //利用随机数,生成长度为8的随机数
String fileImageName = fileUtils.requestUploadImages(file[0], path, filename); //调用图片上传的方法,将图片名,图片,图片路径等参数带上
/**
*
*
* @param file 文件
* @param path 路径
* @param filename 文件名 不含后缀名
* @return uploadfileName 需保存数据
*/
@SuppressWarnings({ "unlikely-arg-type", "unused" })
public String requestUploadImages(MultipartFile file, String path, String filename) {
log.getLogger("fileUtils_s").info("requestUploadImages-start({})", file, path, filename);
String requestParams = null;
String uploadfileName = null;
boolean status = false;
Map<String, String> requestMap = new HashMap<>();
Map<String, Object> resultMap = new HashMap<>();
try {
if (file == null) {
throw new IllegalStateException("上传文件为空,上传失败~");
}
else {
// 组装请求
ObjectMapper mapper = new ObjectMapper();
//上传图片的接口的路径
String url = utilService.getHKCDomain() + UPLOAD_URL_DEV;
// 文件名
HttpPost httpPost = new HttpPost(url);// 测试
// HttpPost httpPost = new HttpPost(UPLOAD_URL_OFFICIAL);// 正式
// 构建MultipartEntityBuilder参数
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(java.nio.charset.Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, filename);
builder.addTextBody("path", path);
builder.addTextBody("filename", filename);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpClient httpClient = HttpClientBuilder.create().build();
// 发送请求
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String entityStr = EntityUtils.toString(responseEntity);
log.getLogger("fileUtils_s").info("requestUploadImages-response req:({}),res:({})", requestMap, entityStr);
JsonNode jsonNode = mapper.readTree(entityStr);
JsonNode responseNode = jsonNode.get("Response");
HKCAPIResponse pr = mapper.readValue(responseNode.toString(), HKCAPIResponse.class);
// 处理响应
if (pr != null && SUCCESS_CODE.equals(pr.getCode())) {
//调用成功->获取文件全路径
uploadfileName = pr.getData().get("status").toString();
//截取文件名(含后缀)
uploadfileName = uploadfileName.substring(uploadfileName.lastIndexOf("/") + 1);
if (StringUtils.isEmpty(uploadfileName)) {
throw new IllegalStateException("非法文件名");
}
}
else if (pr != null && FIAL_CODE.equals(pr.getCode())) {
throw new IllegalStateException("非法文件类型");
}
else {
// 无效响应数据
log.getLogger("fileUtils_s").error("requestUploadImages-invalidResponse({},{})", requestMap, entityStr);
throw new IllegalStateException("cos接口异常");
}
}
}
catch (IllegalStateException e) {
log.getLogger("fileUtils_s").error("requestUploadImages-illegalState({},{})", requestMap, e.getMessage());
throw new IllegalStateException("cos接口异常");
}
catch (JsonProcessingException e) {
log.getLogger("fileUtils_s").error("requestUploadImages-JsonException({},{})", requestMap, e.getMessage());
throw new IllegalStateException("cos接口异常");
}
catch (ClientProtocolException e) {
log.getLogger("fileUtils_s").error("requestUploadImages-httpClientException({},{})", requestMap, e.getMessage());
throw new IllegalStateException("cos接口异常");
}
catch (IOException e) {
log.getLogger("fileUtils_s").error("requestUploadImages-IOException({},{})", requestMap, e.getMessage());
throw new IllegalStateException("cos接口异常");
}
catch (Exception e) {
log.getLogger("fileUtils_s").error("requestUploadImages-Exception({},{})", requestMap, e.getMessage());
throw new IllegalStateException("cos接口异常");
}
return uploadfileName;
}
}