栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

vue+java图片上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue+java图片上传

文章目录
  • 前言
      • 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!*
**欢迎点赞、关注、留言,一起学习、交流!**
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号