博客
关于我
利用spring 实现文件上传、下载
阅读量:506 次
发布时间:2019-03-07

本文共 3470 字,大约阅读时间需要 11 分钟。

Spring框架中的FileCopyUtils类提供了文件拷贝的功能,同时可以将拷贝结果输出到HttpServletResponse中,从而实现文件下载。以下是关于文件上传和下载的详细说明。

文件上传需要使用HTML表单,并设置form的enctype属性为multipart/form-data。该属性能够确保表单数据被正确解析为多部分内容,支持文件附件的上传。此外,文件框的ID即为fileKey,前端示例中文件输入框的name属性应设置为fileKey。

以下是前端示例的具体实现:

在后端处理中,文件上传可以通过Spring的MultipartHttpServletRequest类来实现。以下是文件上传的具体逻辑:

public class UploadFileName {    public String allPathName;    public String name;}public String fileUpLoad(HttpServletRequest request, String fileKey, String desFileName) {    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    CommonsMultipartFile cfile = (CommonsMultipartFile) multipartRequest.getFile(fileKey);    File fo = null;    try {        fo = new File(desFileName);        cfile.getFileItem().write(fo);    } catch (Exception e) {        throw new SystemException(e.getMessage());    }    return desFileName;}public UploadFileName fileUpLoad(HttpServletRequest request, String fileKey, String desFilePath, String DesFileName) {    UploadFileName r = new UploadFileName();    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    CommonsMultipartFile cfile = (CommonsMultipartFile) multipartRequest.getFile(fileKey);    File dir = new File(desFilePath + File.separator);    if (!dir.exists()) {        dir.mkdirs();    }    String fileName = cfile.getOriginalFilename();    String fix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();    fileName = desFilePath + File.separator + DesFileName + fix;    r.allPathName = fileName;    r.name = DesFileName + fix;    File fo = null;    try {        fo = new File(fileName);        cfile.getFileItem().write(fo);    } catch (Exception e) {        throw new SystemException(e.getMessage());    }    return r;}public void fileDownLoad(HttpServletResponse response, String filePath, String fileName, String saveFileName) throws IOException {    InputStream fis = null;    try {        File file = new File(filePath + fileName);        if (!file.exists()) {            throw new SystemException("文件不存在");        }        fis = new BufferedInputStream(new FileInputStream(filePath + fileName));        String f = saveFileName.equals("") ? fileName : saveFileName;        response.setContentType("application/x-msdownload;");        response.setHeader("Content-disposition", "attachment; filename=" + new String(f.getBytes("GB2312"), "ISO-8859-1"));        response.setContentType("application/" + fileName.substring(fileName.lastIndexOf(".") + 1));        FileCopyUtils.copy(fis, response.getOutputStream());    } finally {        if (fis != null) {            try {                fis.close();            } catch (Exception e) {                e.printStackTrace();            }        }    }}public void fileDownLoad(HttpServletResponse response, String fileName) throws IOException {    String fileAll = FilePatch.getProjectPatch() + File.separator + fileName;    fileAll = fileAll.replace("/", File.separator);    String filepath = fileAll.substring(0, fileAll.lastIndexOf(File.separator) + 1);    String name = fileAll.substring(fileAll.lastIndexOf(File.separator) + 1);    fileDownLoad(response, filepath, name, name);}public void fileDel(String fileName) {    File file = new File(fileName);    if (file.exists()) {        file.delete();    }}

以上代码实现了文件的完整上传和下载流程,确保了文件的安全性和操作的可靠性。通过合理设置表单属性和使用Spring框架的MultipartHttpServletRequest类,可以实现文件的异步上传和高效下载。

转载地址:http://hyrjz.baihongyu.com/

你可能感兴趣的文章
PIP使用SSH从BitBucket安装自定义软件包,无需输入SSH密码
查看>>
pip命令提示unknow or unsupported command install解决方法
查看>>
pip在安装模块时提示Read timed out
查看>>
pip更换源
查看>>
SpringBoot之Banner源码深度分解
查看>>
Pix2Pix如何工作?
查看>>
QuickBI助你成为分析师——搞定数据源
查看>>
pkl来存储python字典
查看>>
quick sort | 快速排序 C++ 实现
查看>>
pkpmbs 建设工程质量监督系统 Ajax_operaFile.aspx 文件读取漏洞复现
查看>>
pkpmbs 建设工程质量监督系统 文件上传漏洞复现
查看>>
pku 2400 Supervisor, Supervisee KM求最小权匹配+DFS回溯解集
查看>>
queue队列、deque双端队列和priority_queue优先队列
查看>>
PKUSC2018游记
查看>>
PK项目测试,做产品测试有这4大优势!
查看>>
pl sql 的目录 所在的目录 不能有 小括号,如 Program Files (x86)
查看>>
PL SQLDEVELOPMENT导出数据库脚本
查看>>
Queue
查看>>
PL/SQL Developer中文版下载以及使用图解(绿色版)
查看>>
pl/sql developer乱码,日期格式等问题解决
查看>>