您的位置:首页 > 其它

选中多文件同时上传(利用input元素的multiple属性)

2016-05-09 10:45 477 查看
网页:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<p>一个选择上传的文件</p>
<input type="text" name="reck" value=""/><br/>
<!-- 可以选取一张或者多种图片上传 -->
<input type="file" name="files" multiple/><br/>
<!-- 每个input元素只能选中一个文件 -->
<!--        <input type="file" name="files" /><br/>
<input type="file" name="files" /><br/>
<input type="file" name="files" /><br/> -->
<input type="submit" value="Submit"/>
</form>
</body>
</html>


spring中配置:

<!-- ========文件上传======= -->
<!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>


java代码:

package org.wxy.demo.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;
/**
*
* <一句话功能简述><p>
* MultipartFile类常用的一些方法:
* String getContentType()          //获取文件MIME类型
* InputStream getInputStream()     //后去文件流
* String getName()                 //获取表单中文件组件的名字
* String getOriginalFilename()     //获取上传文件的原名
* long getSize()                   //获取文件的字节大小,单位byte
* boolean isEmpty()                //是否为空
* void transferTo(File dest)       //保存到一个目标文件中。
*
* @author wangxy
* @param <E>
*
* @see  [相关类/方法]
* @since  [产品/模块版本]
*/
@Controller
public class FileUpDownCtrl<E> {

/**
* 上传文件
* @param file
* @return
*/
@RequestMapping(value="/upload.do")
public String fileUpload(@RequestParam("files") MultipartFile[] files,HttpServletRequest request) {
String rect = (String) request.getAttribute("rect");
System.out.println(rect);

//判断file数组不能为空并且长度大于0
if(files!=null && files.length>0){
//循环获取file数组中得文件
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];

try {
//获取存取路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}
// 重定向
return "redirect:/list.do";
}
// 重定向
return "redirect:/fail.html";
}

@RequestMapping(value="/list.do")
@ResponseBody
public String list(HttpServletRequest request,HttpServletResponse response) {
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload";
File[] files = new File(filePath).listFiles();

StringBuilder fileNames = new StringBuilder();
for(File f : files){
System.out.println(f.getName());
fileNames.append(f.getName()+"\n");
}
return fileNames.toString();
}

/**
* 配置了 <property name="resolveLazily" value="true" />
* 才有效
* @author wangxy
*
* @param ex
*/
@ExceptionHandler
public void doExcepiton(Exception ex){
if(ex instanceof MaxUploadSizeExceededException){
System.out.println("文件太大");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多文件上传