有两种方法

导包和上传配置自己搞;

第一种:

上传单个文件:

@RequestMapping("/addfile1")
public String addfile(@RequestParam("file")MultipartFile file)throws Exception{
String path="E:/idea/_1111/src/main/webapp/statics";
String newfile=FilenameUtils.concat(path,file.getOriginalFilename());
file.transferTo(new File(newfile));
return "inputfile";

这个方法的有点就是简单快速,但缺点就是不能更改上传的文件名。原理是直接把文件改变一个储存位置。

第二种:

上传单个文件

 @RequestMapping("/addfile")
public String addfile(@RequestParam("file")MultipartFile file, Model model)throws Exception{
String path="E:/idea/_1111/src/main/webapp/statics";
File updatefile=new File(path);
if (!updatefile.exists()){
updatefile.mkdir();
}
String filename=file.getOriginalFilename();
int intt=filename.lastIndexOf(".");
String str=filename.substring(intt);
String newfilename= UUID.randomUUID()+str;
newfilename=newfilename.replace("-","");
OutputStream out=new FileOutputStream(new File(path+File.separator+newfilename));
InputStream input=file.getInputStream();
byte [] b=new byte[1024];
int len=0;
int temn=0;
while ((temn=input.read())!=-1){
out.write(b,0,temn);
}
out.close();
input.close(); return "inputfile";
}

这种方法是把旧文件读出再写入新文件,再更改一下文件名,使用UUID伪随机。

上传多个文件

@RequestMapping("/addfile")
public String addfile(@RequestParam("file")MultipartFile files[], Model model)throws Exception{
String path="E:/idea/_1111/src/main/webapp/statics";
File updatefile=new File(path);
if (!updatefile.exists()){
updatefile.mkdir();
}
List<String> lii=new ArrayList<String>();
for(int i=0;i<files.length;i++) {
String filename = files[i].getOriginalFilename();
int intt = filename.lastIndexOf(".");
String str = filename.substring(intt);
String newfilename = UUID.randomUUID() + str;
newfilename = newfilename.replace("-", ""); OutputStream out = new FileOutputStream(new File(path + File.separator + newfilename));
InputStream input = files[i].getInputStream();
byte[] b = new byte[1024];
int temn = 0;
while ((temn = input.read(b)) != -1) {
out.write(b, 0, temn);
}
out.close();
input.close();
lii.add(newfilename);
}
model.addAttribute("file",lii); return "inputfile"; }

可以插入多个文件。用一个file数组接收。

ssm框架文件上传的更多相关文章

  1. SSH,SSM框架文件上传

    一.了解文件上传 1.1        什么是文件上传 将本地文件通过流的形式写到服务器上 1.2        文件上传的技术 JspSmartUpload: 其组件是应用jsp进行B/S程序开发过 ...

  2. 文件上传—SSM框架文件上传

    1.准备上传下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>common ...

  3. ssh框架文件上传下载

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 第二百七十三节,Tornado框架-文件上传

    Tornado框架-文件上传 第一.普通表单上传文件 self.request.files["fafafa"] 获取上传文件信息,参数["上传文件框的name名称&quo ...

  5. tp框架-----文件上传

    之前也做过文件上传,现在学了tp,用tp怎么做呢? 第一步:做一个Wenjian控制器: <?php namespace Ceshi\Controller; use Think\Controll ...

  6. tp 框架 -文件上传

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?ph ...

  7. SSM实现文件上传

    1.导入上传需要的jar包 commons-fileupload-1.3.3.jar commons-io-2.6.jar 2.创建 index.jsp <%@ page contentType ...

  8. Android采取async框架文件上传

    页面效果 须要的权限 <uses-permission android:name="android.permission.INTERNET"/> 网络訪问权限; 布局文 ...

  9. 文件上传—SSH框架文件上传

    1.准备上传的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>commons- ...

随机推荐

  1. 【Oracle】安装注意事项

    装了卸载,卸载装,一会儿缺少配置功能,一会对一些莫名的命令操作不能顺利执行.于是还是选择了重装系统.(策略,与其纠结那些个抛错命令和那些烦人的长长的日志,不如重新装系统,这个绝对是最省时间的) 1.安 ...

  2. bzoj1050旅行

    题目链接 其实没有辣么难, 暴力枚举最小边是哪条边,然后每次跑一边最小生成树, 当$s,t$刚好联通时最后加的边的权值就是当前的最大边最小的情况 然后判断一下,更新答案就好 /************ ...

  3. CSS单行超长溢出如何处理?表格某一行某一列超长如何处理?

    表格某一行某一列超长 截取一部分 并增加...效果 增加titile ======================================================== <td t ...

  4. Ionic2文档整理

    来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...

  5. 经典排序的python实现

    具体原理我这里就不解释了,可以查看数据结构课本或者百度百科 这里只给出相应的代码(很简洁) 1 __author__ = "WSX" class sort: def __init_ ...

  6. 返回参数去掉xml格式,以纯json格式返回(转)

    Json 格式显示public static void Register(HttpConfiguration config) { //////////////设置不以xml格式返回 config.Fo ...

  7. list排序问题

    用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: /*** 根据order对User排序*/public class User ...

  8. ThreadFactory类的使用

    之前创建线程的话,基本上是使用new Thread(),或者是将任务提交到线程池执行.今天看了一下洁城浩的<图解java多线程设计模式>突然看到还可以使用ThreadFactory来创建一 ...

  9. CF1012B Chemical table 题解【二分图】【构造】

    有意思的网格图转化.CF Div.1 还是挺有难度的. 注:由于本题有较完美的中文题面,所以不贴英文题面. 英文题面 题目描述 Innopolis 大学的教授正努力研究元素周期表.他们知道,有 \(n ...

  10. ABP相关网站

    ABP的官方网站:http://www.aspnetboilerplate.com ABP在Github上的开源项目:https://github.com/aspnetboilerplate 系列文章 ...