|
@ -5,16 +5,29 @@ import com.yihu.fastdfs.FastDFSUtil;
|
|
|
import com.yihu.jw.exception.business.file_upload.*;
|
|
|
import com.yihu.jw.restmodel.iot.common.UploadVO;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
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.HttpClientBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.csource.common.MyException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Base64;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
/**
|
|
@ -26,6 +39,9 @@ public class FileUploadService {
|
|
|
@Autowired
|
|
|
private FastDFSUtil fastDFSHelper;
|
|
|
|
|
|
@Value("${wlyy.url}")
|
|
|
private String wlyyUrl;
|
|
|
|
|
|
/**
|
|
|
* 文件流上传图片
|
|
|
* @param inputStream
|
|
@ -145,6 +161,76 @@ public class FileUploadService {
|
|
|
return uploadVO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* i健康调用文件传输
|
|
|
* @param multipartFile
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public Map<String, Object> uploadImg(MultipartFile multipartFile) throws Exception {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
long size = multipartFile.getSize();
|
|
|
if(size<=0){
|
|
|
map.put("uploadStatus",1);//size小于0
|
|
|
map.put("accessoryUrl",null);//
|
|
|
return map;
|
|
|
}
|
|
|
String fileName = multipartFile.getOriginalFilename();
|
|
|
String[] fs = fileName.split("\\.");
|
|
|
String type = fs[1];
|
|
|
//图片常见格式:bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp
|
|
|
List img = new ArrayList(Arrays.asList("bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp"));
|
|
|
if (!img.contains(type)) {
|
|
|
map.put("uploadStatus",2);//文件类型不对
|
|
|
map.put("accessoryUrl",null);//
|
|
|
return map;
|
|
|
}
|
|
|
String response = request(wlyyUrl + "/upload/chat", multipartFile, null);
|
|
|
org.json.JSONObject rs = new org.json.JSONObject(response);
|
|
|
Integer status = (Integer) rs.get("status");
|
|
|
if (status == 200) {
|
|
|
String url = rs.get("urls") + "";
|
|
|
map.put("uploadStatus", 0);//文件类型正确
|
|
|
map.put("accessory", url);//
|
|
|
return map;
|
|
|
}
|
|
|
throw new Exception();
|
|
|
}
|
|
|
|
|
|
public String request(String remote_url, MultipartFile file, String type) {
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
String result = "";
|
|
|
try {
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
HttpPost httpPost = new HttpPost(remote_url);
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(type)) {
|
|
|
builder.addTextBody("type", type); //发送类型
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* base64上传图片
|
|
|
* @param jsonData,头像转化后的输入流
|