Spring MultipartFile 上传 与 excel处理
来源:互联网 发布:淘宝街拍图片 编辑:程序博客网 时间:2024/05/13 04:13
转载来源http://blog.csdn.net/libaolin198706231987/article/details/52524133
一、配置
1、*-servlet.xml (WebApploicationContext)
- <!-- file upload -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="5000000"/>
- <property name="maxInMemorySize" value="4096"/>
- <property name="defaultEncoding" value="GBK"/>
- </bean>
- <!-- === excel文件上传页面=== -->
- <bean name="/excelupload.do" class="test.ExcelUpload">
- <property name="formView" value="atest"/>
- <property name="successView" value="atest"/>
- <property name="commandName" value="upload"/>
- <property name="commandClass" value="test.UploadDTO"/>
- </bean>
二、代码
1、使用MultipartFile
- import java.io.Serializable;
- import org.springframework.web.multipart.MultipartFile;
- public class UploadDTOimplementsSerializable{
- private MultipartFile file;
- /**
- * @return Returns the file.
- */
- public MultipartFile getFile(){
- return file;
- }
- /**
- * @param file The file to set.
- */
- public voidsetFile(MultipartFilefile){
- this.file=file;
- }
- }
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Enumeration;
- import java.util.Iterator;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.ServletRequestDataBinder;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.multipart.MultipartHttpServletRequest;
- import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.SimpleFormController;
- public class ExcelUploadextends SimpleFormController{
- protected ModelAndView onSubmit(HttpServletRequestrequest,
- HttpServletResponse response, Object command,BindException errors)
- throws Exception {
- logger.info("Excel upload=================================start");
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
- MultipartFile multipartFile = multipartRequest.getFile("file");
- Enumeration names = request.getParameterNames();
- while (names.hasMoreElements()){
- String name = (String) names.nextElement();
- logger.debug(name+"="+request.getParameter(name));
- }
-
-
- logger.info("Excel upload=================================end");
- return super.onSubmit(request, response,command, errors);
- }
- }
- import java.io.Serializable;
- import org.springframework.web.multipart.MultipartFile;
- public class UploadDTOimplementsSerializable{
- private byte[]file;
- /**
- * @return Returns the file.
- */
- public byte[]getFile(){
- return file;
- }
- /**
- * @param file The file to set.
- */
- public voidsetFile(byte[]file){
- this.file=file;
- }
- }
- public class ExcelUpload extends SimpleFormController{
- protected void initBinder(HttpServletRequest request,
- ServletRequestDataBinder binder)throws ServletException{
- binder.registerCustomEditor(byte[].class,
- new ByteArrayMultipartFileEditor());
- }
- protected ModelAndView onSubmit(HttpServletRequestrequest,
- HttpServletResponse response, Object command,BindException errors)
- throws Exception {
- logger.info("Excel upload=================================start");
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
- MultipartFile multipartFile = multipartRequest.getFile("file");
- logger.debug(Long.toString(multipartFile.getSize()));
- logger.info("Excel upload=================================end");
- return super.onSubmit(request, response,command, errors);
- }
- }
附:
1、另存上传文件
- public void SaveFileFromInputStream(InputStreamstream,String path,
- String filename)throwsIOException{
- FileOutputStream fs = new FileOutputStream(path+"/" + filename);
- byte[]buffer= new byte[1024* 1024];
- int bytesum = 0;
- int byteread = 0;
- while ((byteread = stream.read(buffer))!=-1) {
- bytesum += byteread;
- fs.write(buffer, 0, byteread);
- fs.flush();
- }
- fs.close();
- stream.close();
- }
2、Excel文件处理(POI - 2.5)
- private int getTargetList(MultipartFile multipartFile,
- HttpServletRequest request,Date insuranceBeginTime,String planCode)
- throws Exception {
- String fileType = "";
- try {
- String fileName = multipartFile.getOriginalFilename();
- fileType = fileName.substring(fileName.lastIndexOf(".")+ 1,
- fileName.lastIndexOf(".")+ 4);
- } catch(Exception e){
- logger.error(e);
- fileType = "";
- }
- if (!fileType.toLowerCase().equals("xls")){
- // throw new BusinessServiceException("导入的文件格式不正确,应该不是excel文件");
- this.logger.debug("导入的文件格式不正确,应该不是excel文件");
- return 0;
- }
- HSSFWorkbook wb = null;
- try {
- wb = new HSSFWorkbook(multipartFile.getInputStream());
- // logger.debug(wb.getNumberOfSheets());
- HSSFSheet sheet = wb.getSheetAt(0);
- logger.debug("sheet name = "+wb.getSheetName(0));
- for(int i= sheet.getFirstRowNum();i<=sheet.getLastRowNum();i++){
- HSSFRow row = sheet.getRow(i);
- Iterator cells = row.cellIterator();
- while(cells.hasNext()){
- HSSFCell cell = (HSSFCell) cells.next();
- logger.debug(cell.getStringCellValue());
- }
- }
- logger.debug("last row = "+sheet.getLastRowNum());
-
- return wb.getNumberOfSheets();
- // return sheet.getFirstRowNum();
- } catch(Exception e){
- logger.error(e);
- // throw new
- // BusinessServiceException("未知原因!保存Excel文件时,请不要将鼠标最终定位在Excel中的可以下拉选值的列上。");
- }
- // ApplicationContext ctx =
- // ApplicationWebContexton.getInstance(request);
- // IImportExcelTarget excelParser = (IImportExcelTarget)
- // ctx.getBean(planCode);
- // return excelParser.getTargetList(wb, request,
- // insuranceBeginTime,planCode);
- return 0;
- }
3、request.parameters = 与上传文件同表单的 field value
- <form action="excelupload.do"method="post" enctype="multipart/form-data"name="form1">
- <tablewidth="100%"border="0" cellspacing="1" cellpadding="0">
- <tr>
- <td> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td> </td>
- <td><label>上传
- <input type="file"name="file">
- </label></td>
- <td> </td>
- </tr>
- <tr>
- <td> </td>
- <td> <inputname="assas"type="text"id="assas"value="asdsadas">
- <input type="submit"name="Submit"value="提交"></td>
- <td> </td>
- </tr>
- </table>
- </form>
0 0
- Spring MultipartFile 上传 与 excel处理 .
- Spring MultipartFile 上传 与 excel处理
- Spring MultipartFile 上传 与 excel处理
- Spring MultipartFile 上传 与 excel处理
- Spring MultipartFile 文件上传
- spring boot MultipartFile[]文件上传
- spring实战-Spring文件上传MultipartFile
- Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)
- spring mvc MultipartFile 上传文件错误解决
- MultipartFile实现文件上传(单文件与多文件上传)
- java 上传文件的处理(MultipartFile file)
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- Spring SpringMVC使用MultipartFile实现文件的上传
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- spring boot 使用MultipartFile实现文件上传报错。
- Spring mvc 上传图片MultipartFile 一直为空
- Spring boot上传文件时MultipartFile为空问题
- Spring Cloud Feign Client 实现MultipartFile上传文件功能
- 学习分享js第一课
- BFS算法(详细C)
- Android之 编译错误总结
- 通过面试总结关于java开发者职业发展的一些总结和思考
- [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)
- Spring MultipartFile 上传 与 excel处理
- socket.io 基础使用
- 在网页里.NET控件传递HTML代码时默认是不允许的需要配置添加了ValidateRequest="false"仍然报错的解决办法
- 【webapp】页面开发注意店
- C++习题EasyString
- Python爬虫实战之爬取链家广州房价_02把小爬虫变大
- Linux监控命令整理(top,free,vmstat,iostat,mpstat,sar,netstat)
- 线程暂停和继续实现
- Qt qtableview 多行选中,获取行号