UploadFileUtil

public class UploadFileUtil{


/**

* @Title: uploapImage
* @Description: TODO(图像上传)
* @param request http请求
* @param imgPath 图像路径
* @param thumPath 映像图像路径   不生成映像时,此项为空
* @param tWidth 映像宽度
* @param tHeight 映像高度
* @return
* @throws IOException    设定文件
* @return String    返回类型
* @throws
*/
public static String saveLocalImage(HttpServletRequest request,String imgPath,boolean thumFlag,Integer tWidth,Integer tHeight) throws IOException{

// 转型为MultipartHttpRequest:   
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件:   
        MultipartFile file = multipartRequest.getFile("file");
        // 获得文件名:   
        String filename = file.getOriginalFilename();
        //获取后缀名
        String suffix =  filename.substring(filename.indexOf(".")+1);
        
        String localBasePath = request.getSession().getServletContext().getRealPath("")+imgPath;
Toolkit.buildDirectory(localBasePath+"/image");
Toolkit.buildDirectory(localBasePath+"/thum");
        
        String newFileName = Toolkit.RandomStr()+"."+suffix;
        
        //生成 的本地文件路径、镜像文件以及URI路径
        String localFilePath = localBasePath+"/image/IMG_"+newFileName;
        
        // 写入文件
        File source = new File(localFilePath);
        file.transferTo(source);
        
        
        
        //是否生缩略图并返回缩略图路径
        if(thumFlag){
        String localThumPath = localBasePath+"/thum/TMP_"+newFileName;
       
        //创建缩略图
       InputStream is=new FileInputStream(source);
        try {
       BufferedImage bi=ImageIO.read(is);
       //写入缩略图
       ImageIO.write(PicZoom.zoom(bi,tWidth,tHeight), suffix, new File(localThumPath));
       is.close();
       return (imgPath+"/thum/TMP_"+newFileName).replaceAll("//", "/");
        } finally{
        if(is!=null)is.close();
// TODO: handle exception
}
        }
        
        return (imgPath+"/image/IMG_"+newFileName).replaceAll("//", "/");
}


/**
 * 
 * @Title: saveLocalFile
 * @Description: TODO(保存本地文件)
 * @param request
 * @param filePath 文件路径
 * @throws IOException    设定文件
 * @return void    返回类型
 * @throws
 */
public static String saveLocalFile(HttpServletRequest request,String filePath,String fileName) throws IOException{

// 转型为MultipartHttpRequest:   
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件:   
        MultipartFile file = multipartRequest.getFile("file");
        // 获得文件名:   
        String filename = file.getOriginalFilename();
        //获取后缀名
        String suffix =  filename.substring(filename.indexOf(".")+1);
        
        
        String localBasePath = request.getSession().getServletContext().getRealPath("")+filePath;
        
        if(Toolkit.isEmpty(fileName)){
        fileName = Toolkit.RandomStr()+"."+suffix;
        }else{
        fileName = fileName +"."+suffix;
        }
        
        Toolkit.buildDirectory(localBasePath);
        
        //生成 的本地文件路径、镜像文件以及URI路径
        String localFilePath = (localBasePath+"/"+fileName).replaceAll("//","/").replaceAll("\\\\","/");
        
        // 写入文件
        File source = new File(localFilePath);
        file.transferTo(source);
        
        return (filePath+"/"+fileName).replaceAll("//","/");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值