2021-10-28

从服务器下载文件,和文件上传到服务器

文件下载!!!

 @RequestMapping("/filedownload")
    public R downloadFile(HttpServletRequest request,
                          HttpServletResponse response, @RequestBody String reqbody) throws UnsupportedEncodingException {
     // JSONObject jsonObj=JSONObject.parseObject(reqbody);
     // String fileUrl =JSONTools.getString(jsonObj,"fileUrl");
        //String rootPath = fileFullName;//这里是我在配置文件里面配置的根路径,各位可以更换成自己的路径之后再使用(例如:D:/test)
        //String FullPath = rootPath + fileFullName;//将文件的统一储存路径和文件名拼接得到文件全路径
        String FullPath =fileFullName;//将文件的统一储存路径和文件名拼接得到文件全路径
        File packetFile = new File(FullPath);
        String fileName = packetFile.getName(); //下载的文件名
        File file = new File(FullPath);
        // 如果文件名存在,则进行下载
        if (file.exists()) {
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 实现文件下载
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("Download the song successfully!");
            } catch (Exception e) {
                System.out.println("Download the song failed!");
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {//对应文件不存在
            try {
                //设置响应的数据类型是html文本,并且告知浏览器,使用UTF-8 来编码。
                response.setContentType("text/html;charset=UTF-8");

                //String这个类里面, getBytes()方法使用的码表,是UTF-8,  跟tomcat的默认码表没关系。 tomcat iso-8859-1
                String csn = Charset.defaultCharset().name();

                System.out.println("默认的String里面的getBytes方法使用的码表是: " + csn);

                //1. 指定浏览器看这份数据使用的码表
                response.setHeader("Content-Type", "text/html;charset=UTF-8");
                OutputStream os = response.getOutputStream();

                os.write("对应文件不存在".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
//                return R.error("-1","对应文件不存在");
        }
        return null;
    }

文件上传

@PostMapping("/upload")
    public R singleFileUpload(@RequestParam("file") MultipartFile file,
                                        String urlFile) throws IOException {



        if (file.isEmpty()) {
            return  R.ok("文件为空,请选择你的文件上传");
        }
//        if (!dest.getParentFile().exists()) {
//            dest.getParentFile().mkdir();
//        }

        // 创建一个file对象,指向其文件
        File file1 = new File(urlFile);

        // 为了避免文件创建失败(其所在的文件夹不存在
        // 所以创建它所在的文件目录)
        if (null != file1.getParentFile()) {
            // 创建它所在的文件夹的目录,(该文件夹不存在的话,创建)
            file1.mkdir();
        }
        try{
            Path path=Pahs.get(fileUrl+/file.getOriginalFilename);
            file.transferTo(path);
           String url=path.toString();
        }catch (Exception){
        log.error("上传失败",e);
        }

    retrun R.OK(url);
     
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值