上传下载

@RequestMapping("/updateUpload1")
    public ModelAndView updateUpload1(MultipartFile file, String id,
            String inspect_id) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = new PageData();
        try {
            pd = this.getPageData();
            String filename = null;
            if (Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
                if (null != file && !file.isEmpty()) {
                    // 上传路径
                    String path = "C:/inspect";
                    // 文件名
                    filename = file.getOriginalFilename();
                    File filePath = new File(path, filename);
                    // 判断路径是否存在, 不存在就重新建一个
                    if (!filePath.getParentFile().exists()) {
                        filePath.getParentFile().mkdirs();
                    }
                    file.transferTo(new File(path + File.separator + filename));
                } else {
                    System.out.println("上传失败");
                }
           
            }
            map.put("result", "ok");
        } catch (Exception e) {
            map.put("result", "no");
            logger.error(e.toString(), e);
        }
        return new ModelAndView("save_result");
    }

 

 

@RequestMapping(value = "/download1")
    public void downDocument1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 获取文件下载路径

    //String filePath = PathUtil.getClasspath() + Const.INSPECTFILEPATHIMG + time + File.separator + inspect_document;          //文件上传路径
        String path = "C:/inspect/";
        PageData pageData = new PageData();
        pageData = this.getPageData();
        //获取信息
        PageData data = inspectOrderService.findById(pageData);
        //文件名
        String filename = data.getString("inspect_document");
        String[] strs = filename.split("[.]");
        //截取最后文件属性
        String name = strs[strs.length - 1];
        File file = new File(path + filename);
        String time = data.getString("inspect_document_time").replaceAll("-", "");
        if (file.exists()) {
            // 设置相应类型application/octet-stream
            response.setContentType("application/x-msdownload");
            // 设置头信息
            response.setHeader("Content-Disposition", "attachment;filename=\"" + time + "." + name + "\"");
            InputStream inputStream = new FileInputStream(file);
            ServletOutputStream ouputStream = response.getOutputStream();
            byte b[] = new byte[1024];
            int n;
            while ((n = inputStream.read(b)) != -1) {
                ouputStream.write(b, 0, n);
            }
            // 关闭流、释放资源
            ouputStream.close();
            inputStream.close();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值