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;
    }
    }
  • 相关阅读:
    Jupsh_flutter Android 收不到消息 / 排查不出请根据第 9 点说明提供信息
    angular表单 Dom获取表单值以及双向数据绑定
    angular中的组件以及组件中的模板合成
    Flutter开发的app进行设备判断是Ios还是android
    Java的封装
    GCD编程 之 略微提高篇
    多线程基础(六)GCD基础
    我遇到的CocoaPods的问题(也许后期会解决,持续更新)
    iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
    多线程基础(五)NSThread线程通信
  • 原文地址:https://www.cnblogs.com/vlsion/p/7423893.html
Copyright © 2011-2022 走看看