转自:http://blog.csdn.net/qingwaxw1980/article/details/5017420
一、配置
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>
注意下面的commandClass值,
-
<!-- === 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 UploadDTO implements Serializable {
-
-
private MultipartFile file;
-
-
/**
-
* @return Returns the file.
-
*/
-
public MultipartFile getFile() {
-
return file;
-
}
-
-
/**
-
* @param file The file to set.
-
*/
-
public void setFile(MultipartFile file) {
-
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 ExcelUpload extends SimpleFormController {
-
-
protected ModelAndView onSubmit(HttpServletRequest request,
-
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);
-
-
}
-
}
2、不使用MultipartFile
-
import java.io.Serializable;
-
-
import org.springframework.web.multipart.MultipartFile;
-
-
public class UploadDTO implements Serializable {
-
-
private byte[] file;
-
-
/**
-
* @return Returns the file.
-
*/
-
public byte[] getFile() {
-
return file;
-
}
-
-
/**
-
* @param file The file to set.
-
*/
-
public void setFile(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(HttpServletRequest request,
-
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(InputStream stream, String path,
-
String filename) throws IOException {
-
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">
-
<table width="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> <input name="assas" type="text" id="assas" value="asdsadas">
-
<input type="submit" name="Submit" value="提交"></td>
-
<td> </td>
-
</tr>
-
</table>
-
</form>
阅读(10485) | 评论(0) | 转发(0) |