zoukankan      html  css  js  c++  java
  • Java上传图片工具类

    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * 图片上传工具类
     */
    public class ImageUploadUtil {
    
    
        public static String uploadFile(@RequestParam("file") MultipartFile file, String strPath) throws IOException {
            String fileName = file.getOriginalFilename();//获取文件名
            fileName = getFileName(fileName);
            String filepath = getUploadPath(strPath);
            if (!file.isEmpty()) {
                try (BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(filepath + File.separator + fileName)))) {
                    out.write(file.getBytes());
                    out.flush();
    
                    return fileName;
                } catch (FileNotFoundException e) {
                    return "error";//( "上传文件失败 FileNotFoundException:" + e.getMessage());
                } catch (IOException e) {
                    return "error";//( "上传文件失败 IOException:" + e.getMessage());
                }
            } else {
                return "error";//( "上传文件失败,文件为空");
            }
        }
    
        /**
         * 文件名后缀前添加一个时间戳
         */
        private static String getFileName(String fileName) {
            int index = fileName.lastIndexOf(".");
            final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyymmddHHmmss");  //设置时间格式
            String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
            fileName = fileName.substring(0, index) + "_" + nowTimeStr + fileName.substring(index);
            return fileName;
        }
    
        /**
         * 获取当前系统路径
         */
        private static String getUploadPath(String strPath) throws IOException {
          
            File file = new File(strPath);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            if (!file.exists()) file.mkdirs();
    
            return file.getAbsolutePath();
    
        }
    }
  • 相关阅读:
    学习web前端怎样入门?初学者赶紧看过来!
    web前端教程:CSS 布局十八般武艺都在这里了
    [zhuan]arm中的汇编指令
    adb命令
    [zhuan]使用uiautomator做UI测试
    [zhuan]java发送http的get、post请求
    Android 关于“NetworkOnMainThreadException”出错提示的原因及解决办法
    android getpost代码
    [转]Android 如何根据网络地址获取网络图片方法
    Android Json解析与总结
  • 原文地址:https://www.cnblogs.com/shoose/p/12931731.html
Copyright © 2011-2022 走看看