博客
关于我
SSM框架中实现多文件以及单文件上传
阅读量:773 次
发布时间:2019-03-23

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

kỹ thuật员工写作风格

1. 准备工作

1.1 Trong file pom.xml thêm các phụ thuộc cần thiết

  • commons-io: commons-io: 2.5
  • commons-fileupload: commons-fileupload: 1.3.2

1.2 Trong file spring-mvc.xml thêm solverresolver:

2. Tải đầu ● khăn hai

2.1quivosrpc simplest

Langkah 1: Trong controller backend

@RequestMapping("/upload")public Object upload(HttpSession session, @RequestParam("uploadFile") MultipartFile file) {    boolean bool = false;    ServletContext context = session.getServletContext();    String realPath = context.getRealPath("/upload");    String fileName = UUID.randomUUID().toString().replace("-", "").substring(0, 15) + "_file_" + file.getOriginalFilename();    try {        file.transferTo(new File(realPath + "/" + fileName));        bool = true;    } catch (IOException e) {        e.printStackTrace();    }    return bool ? "success" : "fail";}

2.2 Frontend code

  • Form với enctype="multipart/form-data"
    -ี่ input type="file" có name attribute
  • فرض rằng sử dụng AJAX để gửi yêu cầu上传
$.ajax({    url: "/upload",    type: "POST",    data: new FormData($("#saveUploadForm")[0]),    processData: false,    contentType: false,    success: function(result) {        layer.msg(result);    },    error: function(e) {        layer.msg(e);    }});

3. Tải đầutnh Luc phổ

3.1 Backend code

private String uploadFile(String webPath, MultipartFile file, HttpSession session) {    ServletContext context = session.getServletContext();    String realPath = context.getRealPath(webPath);    String fileName = UUID.randomUUID().toString().replace("-", "").substring(0, 15) + "_file_" + file.getOriginalFilename();    try {        File file1 = new File(realPath);        if (!file1.exists()) {            file1.mkdirs();        }        file.transferTo(new File(realPath + "/" + fileName));        return webPath + "/" + fileName;    } catch (IOException e) {        e.printStackTrace();        return null;    }}

3.2 Backend controller

@RequestMapping("/upload")public Object upload(@RequestParam("file") MultipartFile[] file, HttpSession session) {    for (int i = 0; i < file.length; i++) {        MultipartFile multipartFile = file[i];        String uploadFilePath = uploadFile("/upload", multipartFile, session);        System.out.println(uploadFilePath);    }    return "success";}

3.3 Frontendcode

  • Dùng AJAX để gửi yêu cầu upload
$.ajax({    url: "/upload",    type: "POST",    data: new FormData($("#saveFileForm")[0]),    processData: false,    contentType: false,    success: function(result) {        layer.msg(result);    },    error: function(e) {        layer.msg(e);    }});

4. Upload file withprogressbar

5. Ảnh hưởng

  • ẢNH này cho thấy quá trình upload file với thanh tiến trình trong suốt
    -vatLooks good!

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

你可能感兴趣的文章
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>