java 文件上传和下载

文件上传: 

(通过userType判断需要下载的文件,可忽略)

    @RequestMapping(value = "/v1/user/template/download/{userType}", method = RequestMethod.GET)
    public Object  downloadBatchRegistUserTemplateFile(@PathVariable Integer userType,  HttpServletResponse resp){
        logger.info("OrganUserController.downloadBatchRegistUserTemplateFile>>>>>>userType:" + userType );
        if (userType == null ) {
            return "参数为空";
        }
        // path是指下载的模板文件的路径
        String path = "D:/RATemplate";
        // name 模板文件名
        String name = null;
        if(userType == Constants.USER_TYPE_PERSON_1){
            name = "个人用户.xlsx";
        }
        if(userType == Constants.USER_TYPE_ORGAN_2){
           name =  "机构用户.xlsx";
        }
        if(userType == Constants.USER_TYPE_DEVICE_3){
           name = "设备用户.xlsx";
        }
        String filename = path + "/" + name;
        System.out.println(filename);
        try {
            resp.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(name,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            ServletOutputStream out = resp.getOutputStream();
            FileInputStream fis = new FileInputStream(filename);
            int len;
            byte[] b = new byte[1024];
            while((len=fis.read(b))!= -1){
                out.write(b, 0 ,len);
            }
            fis.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Result result = new Result();
        logger.info("OrganUserController.downloadBatchRegistUserTemplateFile>>>>>>result:" + JsonUtils.object2Json(result));
        return result.getInfo();
    }

文件上传:

    @RequestMapping(value = "/v1/user/organ/regist/template", method = RequestMethod.POST)
    public Object batchRegistOrganUserByTemplateFile(@RequestParam("file") MultipartFile file, HttpServletResponse resp) {
        logger.info("OrganUserController.batchRegistOrganUserByTemplateFile>>>>>>file:" + file);
        if (file.isEmpty()) {
            return "上传失败,请重新选择文件";
        }
        Result result = new Result();
        //获取文件名
        String fileName = file.getOriginalFilename();
        //保存路径
        String filePath = "D:/UserRegistFile";
        File dest = new File(filePath + "/"+ fileName);
        try {
            file.transferTo(dest);
            logger.info("上传成功");

        } catch (IOException e) {
            logger.error(e.toString(), e);
            return "上传失败!";
        }
        logger.info("OrganUserController.batchRegistOrganUserByTemplateFile>>>>>>result:" + JsonUtils.object2Json(result));
        return result.getInfo();
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值