文件上传和下载Fileupload

upload
远程上传

public static String remoteUpload(String url, String uploadFilePath, String types) throws IOException {
        URL objectUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)objectUrl.openConnection();
        conn.connect();
        InputStream inputStream = conn.getInputStream();
        File newFile = new File(uploadFilePath + SnowflakeIdGenerator.nextId() + types);
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(newFile);

        int index;
        while((index = inputStream.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }

        inputStream.close();
        downloadFile.close();
        return newFile.getPath().replaceAll("\\\\", "/").replace(uploadFilePath, "/upload");
    }

上传

 public static String upload(File file, String uploadFilePath) {
        LocalDate var10000 = LocalDate.now();
        String date = "/" + var10000.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/";
        File path = new File(uploadFilePath + date);
        if (!path.exists()) {
            path.mkdirs();
        }

        File newFile = new File(uploadFilePath + date + SnowflakeIdGenerator.nextId() + StringUtil.getFileType(file));

        try {
            if (!newFile.exists()) {
                newFile.createNewFile();
            }

            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(newFile);
            byte[] data = new byte[8192];
            boolean var8 = false;

            int len;
            while((len = fis.read(data)) != -1) {
                fos.write(data, 0, len);
            }

            fis.close();
            fos.close();
        } catch (IOException var12) {
            var12.printStackTrace();
        } finally {
            MultipartFileToFile.delteTempFile(file);
        }

        return newFile.getPath().replaceAll("\\\\", "/").replace(uploadFilePath, "/upload");
    }

删除

    public static void deleteFile(String uploadFilePath, String[] path) {
        if (path != null && path.length > 0) {
            File file = null;
            String[] var3 = path;
            int var4 = path.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String p = var3[var5];
                file = new File(uploadFilePath + p.replace("/upload", ""));
                if (file.exists()) {
                    file.delete();
                }
            }
        }

    }

Down

@GetMapping("down")
    public void down(String fileName, String path) throws IOException {
        if (fileName == null) {
            throw new ExRuntimeException("文件不存在");
        } else {
            String var10002 = this.config.getUploadPath();
            File file = new File(var10002 + path.replace("/upload", ""));
            if (file.exists()) {
                this.response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                this.response.setContentType("application/octet-stream");
                this.response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;

                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = this.response.getOutputStream();

                    for (int i = bis.read(buffer); i != -1; i = bis.read(buffer)) {
                        os.write(buffer, 0, i);
                    }
                } catch (Exception var12) {
                    throw new ExRuntimeException(var12.getMessage());
                } finally {
                    if (bis != null) {
                        bis.close();
                    }

                    if (fis != null) {
                        fis.close();
                    }

                }
            }
        }
    }

下载工具类

@bean
public class YmlConfig {
    @value("edp.config.uploadPath")
    private String uploadPath;
    @value("edp.config.uploadWinPath")
    private String uploadWinPath;

    public YmlConfig() {
    }

    public String getUploadPath() {
        String props = System.getProperties().getProperty("os.name");
        if (props.toLowerCase().contains(ZdConst.windows)) {
            return this.uploadWinPath != null ? this.uploadWinPath : " C:/data/upload/";
        } else {
            return this.uploadPath != null ? this.uploadPath : "/root/upload/";
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值