zoukankan      html  css  js  c++  java
  • 文件上传

    public class UploadFile {
    private static Logger logger = LoggerFactory.getLogger(UploadFile.class);
    /**
    * @Title: uploadImage
    * @Description: 上传图片
    * @param: MultipartFile
    * @return: 文件地址
    * @throws
    */
    public static String uploadImage(String workPath,MultipartFile files,String name) throws BusinessException {
    if (!files.isEmpty()) {
    try {

    String absolutePath = workPath + File.separator +name+ File.separator +"images";
    logger.info("上传图片的地址为-----------------------------"+absolutePath);
    //String absolutePath = "/Users/zhuqiang/Documents/images";
    // 传文件
    File f = new File(absolutePath, files.getOriginalFilename());
    files.transferTo(f);
    // System.out.println(f.length());
    if (f.length()>100*1024) {
    //压缩图片
    ImageCompression image = new ImageCompression();
    try {
    image.saveImageAsJpg(f, absolutePath + File.separator + files.getOriginalFilename(), 250, 150);
    } catch (Exception e) {
    throw new BusinessException(BusinessException.CODE_ERROR, "图片压缩失败");
    }
    }
    //files.transferTo(new File("d://upload//images//saler",files.getOriginalFilename()));
    return files.getOriginalFilename();
    } catch (IOException e) {
    logger.info("服务器地址异常-----------------------------"+e.getMessage());
    throw new BusinessException(BusinessException.CODE_ERROR,"服务器地址异常");
    }
    } else {
    throw new BusinessException(BusinessException.CODE_ERROR,"上传文件数据为空");
    }

    }

    /**
    * @Title: uploadImage1
    * @Description: 上传图片(压缩图片的尺寸是400,300)
    * @param: MultipartFile
    * @return: 文件地址
    * @throws
    */
    public static String uploadImage1(String workPath,MultipartFile files) throws BusinessException {
    if (!files.isEmpty()) {
    try {

    String absolutePath = workPath ;
    // 传文件
    files.transferTo(new File(absolutePath,files.getOriginalFilename()));
    //压缩图片
    ImageCompression image = new ImageCompression();
    try {
    File f = new File(absolutePath,files.getOriginalFilename());
    image.saveImageAsJpg(f,absolutePath+ File.separator+files.getOriginalFilename(), 1000, 800);
    } catch (Exception e) {
    throw new BusinessException(BusinessException.CODE_ERROR,"图片压缩失败");
    }

    //files.transferTo(new File("d://upload//images//saler",files.getOriginalFilename()));
    return files.getOriginalFilename();
    } catch (IOException e) {
    throw new BusinessException(BusinessException.CODE_ERROR,"服务器地址异常");
    }
    } else {
    throw new BusinessException(BusinessException.CODE_ERROR,"上传文件数据为空");
    }

    }

    /**
    * @Title: uploadFile
    * @Description: 上传文件
    * @param: MultipartFile
    * @return: 文件地址
    * @throws
    */
    public static String uploadFile(String workPath,MultipartFile files,String path) throws BusinessException {
    if (!files.isEmpty()) {
    try {
    String absolutePath = workPath;
    // 传文件
    // String absolutePath = "d://upload//meetings//file";
    files.transferTo(new File(absolutePath,files.getOriginalFilename()));
    return files.getOriginalFilename();
    } catch (IOException e) {
    throw new BusinessException(BusinessException.CODE_ERROR,"服务器地址异常");
    }
    } else {
    throw new BusinessException(BusinessException.CODE_ERROR,"上传文件数据为空");
    }

    }

    /**
    * @Title: uploadVideos
    * @Description: 上传视频
    * @param: MultipartFile
    * @return: 文件地址
    * @throws
    */
    public static String uploadVideos(String workPath,MultipartFile files) throws BusinessException {
    if (!files.isEmpty()) {
    try {
    String absolutePath = workPath ;
    // 传文件
    files.transferTo(new File(absolutePath,files.getOriginalFilename()));
    // files.transferTo(new File("d://upload//meetings//file",files.getOriginalFilename()));
    return files.getOriginalFilename();
    } catch (IOException e) {
    throw new BusinessException(BusinessException.CODE_ERROR,"服务器地址异常");
    }
    } else {
    throw new BusinessException(BusinessException.CODE_ERROR,"上传视频数据为空");
    }

    }

    /**
    * @Title: getFileNameNew
    * @Description: 重命名图片
    * @param
    * @return
    * @throws
    */
    public static String getFileNameNew() {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    return fmt.format(new Date());
    }

    /**
    * @Title: addAppImage
    * @Description: 上传app头像
    * @param: workPath,image
    * @return
    * @throws
    */
    public static String addAppImage(String workPath,String image){
    String webPath = workPath + File.separator +"sharemsg"+ File.separator +"images";
    String relativePath = getFileNameNew() + ".jpg";
    File file = new File(webPath,relativePath);
    // image
    byte[] bytes = Base64.decodeBase64(image);
    for (int i = 0; i < bytes.length; ++i) {
    if (bytes[i] < 0) {// 调整异常数据
    bytes[i] += 256;
    }
    }
    FileOutputStream os;
    try {
    os = new FileOutputStream(file);
    os.write(bytes);
    os.flush();
    os.close();
    } catch (Exception e) {
    return "";
    }
    return relativePath;
    }

    /**
    * @Title: addAppFile
    * @Description: 上传app头像
    * @param: workPath,image
    * @return
    * @throws
    */
    public static String addAppFile(String workPath,String fileUrl){
    String webPath = workPath + File.separator +"sharemsg"+ File.separator +"file";
    String relativePath = getFileNameNew() + ".jpg";
    File file = new File(webPath,relativePath);
    // file
    byte[] bytes = Base64.decodeBase64(fileUrl);
    for (int i = 0; i < bytes.length; ++i) {
    if (bytes[i] < 0) {// 调整异常数据
    bytes[i] += 256;
    }
    }
    FileOutputStream os;
    try {
    os = new FileOutputStream(file);
    os.write(bytes);
    os.flush();
    os.close();
    } catch (Exception e) {
    return "";
    }
    return relativePath;
    }
    }
  • 相关阅读:
    利用Airtest开发爬虫
    添加mitmproxy证书为安卓系统证书(模拟器亦可)
    mac下 安卓模拟器抓包推荐
    mac上appium连接ios
    HomeBrew和HomeBrew Cask的安装和使用
    NOIP2020 游记
    CSP2020 游记
    随机数之 xorShift128Plus 板子
    最大单词长度乘积(力扣第318题)
    两个整数之和(力扣第371题)
  • 原文地址:https://www.cnblogs.com/vlsion/p/7423893.html
Copyright © 2011-2022 走看看