通用的导入文件后,生成对应的url路径

@RequestMapping(value = "importEmp", method = RequestMethod.POST)
    public ResponseEntity<?> importEmp( HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
        //导入参数:文件名,文件。
        //文件保存目录路径
        String savePath = session.getServletContext().getRealPath("/") + "attached/";

        //文件保存目录URL
        String saveUrl = request.getContextPath() + "/attached/";

        //定义允许上传的文件扩展名
        HashMap<String, String> extMap = new HashMap<String, String>();
        extMap.put("image", "gif,jpg,jpeg,png,bmp,pdf");
        extMap.put("flash", "swf,flv");
        extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
        extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
        extMap.put("pdf", "pdf");

        //最大文件大小
        long maxSize = 999999999;

        response.setContentType("text/html; charset=UTF-8");

        //检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            return new ResponseEntity("上传目录不存在", HttpStatus.OK);
        }
        //检查目录写权限
        if (!uploadDir.canWrite()) {
            return new ResponseEntity("上传目录没有写权限", HttpStatus.OK);
        }

        String dirName = request.getParameter("dir");
        if (dirName == null) {
            dirName = "image";
        }
        if (!extMap.containsKey(dirName)) {
            return  new ResponseEntity("目录名不正确", HttpStatus.OK);
        }
        //创建文件夹
        savePath += dirName + "/";
        saveUrl += dirName + "/";
        File saveDirFile = new File(savePath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + "/";
        saveUrl += ymd + "/";
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        MultipartHttpServletRequest request1=(MultipartHttpServletRequest)request;
        String file = request1.getFileMap().keySet().iterator().next();
        CommonsMultipartFile f=(CommonsMultipartFile)request1.getFileMap().get(file);
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");


        JSONObject obj = new JSONObject();
        FileItem item = f.getFileItem();
        String fileName = item.getName();
        long fileSize = item.getSize();
        if (!item.isFormField()) {
            //检查文件大小
            if (item.getSize() > maxSize) {
                return new ResponseEntity("上传文件大小超过限制", HttpStatus.OK);
            }
            //检查扩展名
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            if (!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)) {
                return new ResponseEntity("上传文件扩展名是不允许的扩展名。\\n只允许\" + extMap.get(dirName) + \"格式。", HttpStatus.OK);
            }

            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
            try {
                File uploadedFile = new File(savePath, newFileName);
                item.write(uploadedFile);
            } catch (Exception e) {
                return new ResponseEntity("上传文件失败", HttpStatus.OK);
            }

            obj.put("url", saveUrl + newFileName);
        }
        return new ResponseEntity(obj.get("url"), HttpStatus.OK);
    }

请求:

总结:

导入文件后 ,会在项目下一个目录里生成这个文件,后读取文件数据流到这个文件,生成对应的url返回给前端
总的来说,就是在项目下生成对应的文件,后给前端返回这个文件的路径,将路径储存在数据库里。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值