博客
关于我
利用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/

你可能感兴趣的文章
pytorch中的求和函数sum(axis=x)解释
查看>>
Problem F: 质心算法
查看>>
Problem N HDU 2612 Find a way (两次BFS求最值)
查看>>
Process /usr/libexec/gdu-notification-daemon was killed by signal 6 (SIGABRT)
查看>>
process.env.VUE_APP_BASE_API 获取不到
查看>>
Process.run() 和 Process.start() 之间的区别
查看>>
Processes
查看>>
Processing通过编程实现艺术设计_实现艺术和现实的交互---数据设计分析002
查看>>
ProcessOnLoading
查看>>
SpringBoot中集成screw(螺丝钉)实现数据库表结构文档生成
查看>>
PROFINET 模拟器使用教程
查看>>
Program type already present: android.support.v4.widget.EdgeEffectCompat
查看>>
PyTorch中文版官方教程来啦(附下载)
查看>>
Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
查看>>
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
Project Euler 15 Lattice paths
查看>>
Project Euler 48 Self powers( 大数求余 )
查看>>
Project Euler Problem 12: Highly divisible triangular number
查看>>
ProjectEuler 2
查看>>
projection介绍及EPSG:4326和EPSG:3857的投射转换
查看>>