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

你可能感兴趣的文章
php csv 导出
查看>>
php curl 实例+详解
查看>>
php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
查看>>
php curl_multi批量发送http请求
查看>>
php echo 输出 锘?... 乱码问题
查看>>
ReferenceQueue的使用
查看>>
php flush()刷新不能输出缓冲的原因分析
查看>>
Referenced classpath provider does not exist: org.maven.ide.eclipse.launchconfig
查看>>
Refactoring-Imporving the Design of Exsiting Code — 代码的坏味道
查看>>
PHP imap 远程命令执行漏洞复现(CVE-2018-19518)
查看>>
php include和require
查看>>
ref 和out 区别
查看>>
php JS 导出表格特殊处理
查看>>
php json dom解析
查看>>
ReentrantReadWriteLock读写锁解析
查看>>
php laravel实现依赖注入原理(反射机制)
查看>>
php laravel请求处理管道(装饰者模式)
查看>>
ReentrantReadWriteLock读写锁底层实现、StampLock详解
查看>>
PHP mongoDB 操作
查看>>
ReentrantLock读写锁
查看>>