关于断点续传

关于上传文件


```java
  @Override
    public void updateFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
        String body = scanner.hasNext() ? scanner.next() : "";
        JSONObject jsonObj = JSONObject.parseObject(body);
        log.info("在线编辑器回调参数:{}", jsonObj);
        if((int) jsonObj.get("status") == 2|| (int) jsonObj.get("status") == 6) {
            String downloadUri = (String) jsonObj.get("url");
            String id = jsonObj.getString("key");
            String[] value = id.split("=");
            FileDataEntity dataEntity = baseMapper.selectById(value[0]);
            URL url = new URL(downloadUri);
            java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
            @Cleanup InputStream stream = connection.getInputStream();
            String path = System.getProperty("user.dir");
            /*
            log.info(dataEntity.getName(),dataEntity.getType());
            File savedFile = new File(path+"/template/"+dataEntity.getName());
            try (FileOutputStream out = new FileOutputStream(savedFile)) {
                int read;
                final byte[] bytes = new byte[1024];
                while ((read = stream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
            }
            */
            //将文件刷回文件存储服务中
            // 创建fileItem
            File file = new File(path + "/template/" + dataEntity.getId() +"-" + dataEntity.getName());
            FileItem fileItem = new DiskFileItem(
                    "file", // 表单参数名
                    ContentType.APPLICATION_FORM_URLENCODED.getMimeType(), // 文件类型
                    false, // 是否为表单格式
                    file.getName(), // 文件名
                    10240, // 超过多少byte存在磁盘上
                    file
            );
            //将更新的文件流写入到缓存的文件中,然后提交给文件服务
            @Cleanup OutputStream outputStream = fileItem.getOutputStream();
            IOUtils.copy(stream, outputStream);
            connection.disconnect();
            log.info("更新的文件大小:" + fileItem.getSize()+"");
            MultipartFile mfile = new CommonsMultipartFile(fileItem);
            log.info(String.valueOf(mfile.getSize()),mfile.getBytes());
            try {
                attachmentApi.update("annex", dataEntity.getAttachId(), mfile);
            } catch (DataException e) {
                log.error("文件更新出错了", e);
            }
        }
        writer.write("{\"error\":0}");
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值