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

你可能感兴趣的文章
NR,NF,FNR
查看>>
nrf24l01+arduino
查看>>
nrf开发笔记一开发软件
查看>>
nrm —— 快速切换 NPM 源 (附带测速功能)
查看>>
nrm报错 [ERR_INVALID_ARG_TYPE]
查看>>
NS3 IP首部校验和
查看>>
NSDateFormatter的替代方法
查看>>
NSError 的使用方法
查看>>
NSGA-Ⅲ源代码
查看>>
nsis 安装脚本示例(转)
查看>>
NSJSON的用法(oc系统自带的解析方法)
查看>>
nslookup 的基本知识与命令详解
查看>>
NSNumber与NSInteger的区别 -bei
查看>>
NSOperation基本操作
查看>>
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>