• 文件上传


    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;
    }
    }
  • 相关阅读:
    asp.net ajax传递Json给aspx.cs后台 webmethod方法传递json
    新手 Redis 配置笔记(windows),附下载地址
    在 LINQ to Entities 查询中无法构造实体或复杂类型
    WebApi 4.0 默认方式不支持HttpGet 请求
    jQuery .Ajax Error Handling Function
    My97DatePicker 与 某个CSS冲突 ,导致无法显示
    基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
    使用Autofac部署IIS6.0时未能加载文件或程序集“System.Core, Version=2.0.5.0...“
    Could not load file or assembly 'System.Core, Version=2.0.5.0 和autofac冲突的问题
    线程间操作无效: 从不是创建控件“textBox2”的线程访问它
  • 原文地址:https://www.cnblogs.com/vlsion/p/7423893.html
Copyright © 2020-2023  润新知