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

你可能感兴趣的文章
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI将某个程序段耗时插入Excel
查看>>
NPOI格式设置
查看>>
NPOI设置单元格格式
查看>>