- 前言
- 1、流程及图
- 2、前端:
- 1.表单提交
- 2.上传图片及相关方法
- 3、配置文件application.xml
- 3、后端
- 1.控制器
- 2.上传接口
- 3.七牛云上传
- 4.递归压缩图片
- 4、上传如图
- 总结
前言
1、流程及图 2、前端: 1.表单提交
{{editEntity.compayName}}
return fileService.uploadFile(file, Constants.FILE_DIR_CACHET);
}
2.上传接口
private String fileServerSavepath;
private String fileServerHttppath;
public String getFileServerSavepath() {
return fileServerSavepath;
}
public void setFileServerSavepath(String fileServerSavepath) {
this.fileServerSavepath = fileServerSavepath;
}
public String getFileServerHttppath() {
return fileServerHttppath;
}
public void setFileServerHttppath(String fileServerHttppath) {
this.fileServerHttppath = fileServerHttppath;
}
public Result uploadFile(MultipartFile file, String dirName) {
String filePath = dirName + "/" + UUID.randomUUID().toString();
try {
File dest = new File(fileServerSavepath, filePath);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
} catch (IllegalStateException | IOException e) {
String errorMsg = "上传文件[" + file.getOriginalFilename() + "]失败!";
logger.error(errorMsg, e);
return Results.uploadError();
}
return Results.uploadOk(fileServerHttppath + filePath);
}
public Result uploadFileQiniu(MultipartFile file) {
String uuid = UUID.randomUUID().toString();
InputStream is = null;
try {
is = file.getInputStream();
String imgUrl = QiniuUpload.uploadFile(is , uuid);
return Results.uploadOk(imgUrl);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (is != null) {
is.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return Results.error();
}
public Result uploadImageResize(MultipartFile file, String dirName, double destFileSize) {
String uuid = UUID.randomUUID().toString();
String filePath = dirName + "/" + uuid;
try {
File dest = new File(fileServerSavepath, filePath);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
if((Double.isNaN(destFileSize)) || "".equals(destFileSize)){
destFileSize = 15.0;
}
String imgUrl = QiniuUpload.uploadFile(ImageUtil.commpressImageRatio(dest,destFileSize), uuid);
return Results.uploadOk(imgUrl);
} catch (IllegalStateException | IOException e) {
String errorMsg = "上传文件[" + file.getOriginalFilename() + "]失败!";
logger.error(errorMsg, e);
return Results.uploadError();
}
}
3.七牛云上传
public static String uploadFile(InputStream is, String key){
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(qiniuConfig.getRegion());
UploadManager uploadManager = new UploadManager(cfg);
try {
Response response = uploadManager.put(is, key, getAccessToken(), null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return qiniuConfig.getCdnPath() + putRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
return null;
}
4.递归压缩图片
public static InputStream commpressImageRatio(File file,double destFileSize) throws IOException {
final int byteLenght = 1024; // 字节长度
final double minRatio = 0.9; // 压缩比率
double ratio = destFileSize/((double) file.length()/byteLenght);
if(ratio >= minRatio){
return new FileInputStream(file);
}else {
Thumbnails.of(file).scale(0.9).outputQuality(0.9).outputFormat("jpg").toFile(file);
String path = file.getAbsolutePath();
int i = path.lastIndexOf(".")+1;
String suffix = path.substring(i);
if(!suffix.equals("jpg")){
file = new File(file.getAbsolutePath()+".jpg");
File file1 = new File(path);
file1.delete();
}
double ratio1 = destFileSize/((double) file.length()/byteLenght);
if(ratio1 < minRatio){
commpressImageRatio(file,destFileSize);
}
return new FileInputStream(file);
}
}
4、上传如图
总结 *随心所往,看见未来。Follow your heart,see night!*
**欢迎点赞、关注、留言,一起学习、交流!**



