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

    1.工具类FileUtil.java

    package com.h3c.portal.business.seconddev.util;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.util.List;
    
    public class FileUtil {
    
        public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception{
            File targetFile = new File(filePath);
            if(!targetFile.exists()){
                targetFile.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(filePath+fileName);
            out.write(file);
            out.flush();
            out.close();
        }
    
        public static void downloadFiles(List<String> filenames, HttpServletRequest request, HttpServletResponse response) throws Exception {
            try {
                String path = "";
                String filename = "";
                //系统是Linux还是Windows
                String os1 = System.getProperty("os.name");
                if (os1.toLowerCase().startsWith("win")) {
                    //文件名处理乱码
                    filename = new String(filenames.get(0).getBytes("ISO-8859-1"), "UTF-8");
                    //需要下载的文件
                    path = "/D://uploadFile/" + filename;
                } else {
                    //需要下载的文件
                    path = "/uploadFile/" + filenames.get(0);
                }
                System.out.println("文件下载路径-------" + path);
    
                //得到文件输入流
                FileInputStream inputStream = new FileInputStream(new File(path));
    
                //设置头部响应的格式:文件输出时的名字
                response.addHeader("Content-Disposition", "attachment;filename=" + new String(filenames.get(1).getBytes("gb2312"), "ISO8859-1"));
    
                OutputStream os = response.getOutputStream();
    
                response.setContentType("application/octet-stream");
    
                //定义大小 边读边写
                int length = 0;
    
                byte[] bt = new byte[1024];
    
                while ((length = inputStream.read(bt)) > 0) {
                    os.write(bt, 0, length);
                    //重新定义长度
                    bt = new byte[1024];
                }
                //关闭流
                os.flush();
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }

    2.UUID生成工具类

    package com.h3c.portal.business.seconddev.util;
    
    import java.util.UUID;
    
    public class UUIDUtil {
        /**
         * 带-的UUID
         *
         * @return 36位的字符串
         */
        public static String getUUID() {
            return UUID.randomUUID().toString();
        }
    
        /**
         * 去掉-的UUID
         *
         * @return 32位的字符串
         */
        public static String getUUID2() {
            return UUID.randomUUID().toString().replace("-", "");
        }
    }

    3.web层调用或者写入到serviceimpl

       @RequestMapping("uploadFile")
        public BaseResult uploadFile(@RequestParam("file") final MultipartFile file, final HttpServletRequest request) {
            final BaseResult baseResult = new BaseResult(BaseResultEnum.SUCCESS);
            // final String type = request.getParameter("type");
            // String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() +
            // "uploadFile/";
            String path = "";
            final String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) {
                path = "/D://uploadFile/";
            } else {
                path = "/uploadFile/";
            }
            final String fileName = file.getOriginalFilename();
            final String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            final String newFileName = UUIDUtil.getUUID2() + "." + suffix;
            try {
                FileUtil.uploadFile(file.getBytes(), path, newFileName);
            } catch (final Exception e) {
                baseResult.change(BaseResultEnum.FAILD, e.getMessage());
            }
            String requestURL = request.getRequestURL().toString();
            requestURL = requestURL.replace("/workOriginator", "");
            path = requestURL + "/";
            final Map<String, Object> map = new HashMap<>();
            map.put("fileName", fileName);
            map.put("newFileName", newFileName);
            map.put("filePath", path);
            baseResult.setData(map);
            return baseResult;
        }
  • 相关阅读:
    进程与线程
    silverlight中的几个冷门标记 {x:Null},d:DesignWidth,d:DesignHeight
    silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
    silverlight 相册雏型
    IIS7的应用程序池
    silverlight如何在运行时用代码动态控制(或创建)动画
    庆祝silverlight 4 beta版发布,上几张养眼MM照片
    [转贴]Silverlight Socket 实现收发信息
    IIS7.5中神秘的ApplicationPoolIdentity
    silverlight中如何将string(字符串)写入Resource(资源)?
  • 原文地址:https://www.cnblogs.com/Jack-zhao/p/13297209.html
Copyright © 2011-2022 走看看