1.文件上传 jsp
<div id="import-dialog" class="easyui-dialog"
data-options="closed:true,iconCls:'icon-save'"
style="width: 400px; padding: 10px;">
<form id="import-form" method="post" enctype="multipart/form-data">
<div id="create-file-dialog">
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify"/>
<p>
<a id="file-upload" href="javascript:$('#uploadify').uploadify('upload','*')" >上传</a>
<a href="javascript:$('#uploadify').uploadify('cancel','*')">取消上传</a>
</p>
</div>
</form>
</div>
2.文件上传
//导入
function importdata(){
var dialogParent = $('#create-file-dialog').parent();
var dialogOwn = $('#create-file-dialog').clone();
dialogOwn.hide();
$('#create-file-dialog').show();
$('#create-file-dialog').dialog({
title:'添加相关文档',
width:400,
height:200,
closed: false,
cache: false,
modal:false,
onOpen:function(){
$('#uploadify').uploadify({
'uploader':'<%=basePath%>/book/uploadimg.do',
'swf':'<%=basePath%>/js/uploadify.swf',
'cancelImg':'<%=basePath%>/css/images/uploadify-cancel.png',
'queueID':'fileQueue',
'auto':false,
'fileObjName':'file',
'multi':true,
'fileTypeExts': '*.xlsx;',
'buttonText':'选择文件 ',
'onUploadSuccess':function(file,data,response){
alert(file.name+"上传成功!");
}
});
},
onClose:function(){
//初始化dialog
dialogOwn.appendTo(dialogParent);
$(this).dialog("destroy").remove();
}
});
}
3.文件的上传,Action
@Controller
public class BookAction extends BaseAction {
// 上传文件存储目录
private static final String UPLOAD_DIRECTORY = "upload";
// 上传配置
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
@RequestMapping(value = "/book/importdata")
public String bookImportdata(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 检测是否为多媒体上传
if (!ServletFileUpload.isMultipartContent(request)) {
// 如果不是则停止
PrintWriter writer = response.getWriter();
writer.println("Error: 表单必须包含 enctype=multipart/form-data");
writer.flush();
}
// 配置上传参数
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
factory.setSizeThreshold(MEMORY_THRESHOLD);
// 设置临时存储目录
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大文件上传值
upload.setFileSizeMax(MAX_FILE_SIZE);
// 设置最大请求值 (包含文件和表单数据)
upload.setSizeMax(MAX_REQUEST_SIZE);
// 构造临时路径来存储上传的文件
// 这个路径相对当前应用的目录
String uploadPath = request.getSession().getServletContext().getRealPath("./")
+ File.separator + UPLOAD_DIRECTORY;
// 如果目录不存在则创建
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
try {
// 解析请求的内容提取文件数据
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
// 迭代表单数据
for (FileItem item : formItems) {
// 处理不在表单中的字段
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator
+ fileName;
File storeFile = new File(filePath);
item.write(storeFile);
XSSFWorkbook xssfWorkbook = null ;
try {
xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
} catch (Exception e) {
e.printStackTrace();
}
List<Book> list = new ArrayList<Book>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(numSheet);
if (sheetAt == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= sheetAt.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = sheetAt.getRow(rowNum);
if (xssfRow != null) {
Book book = new Book();
//得到列
XSSFCell booknameC = xssfRow.getCell(0);
XSSFCell booktypeC = xssfRow.getCell(1);
XSSFCell bookdescC = xssfRow.getCell(2);
XSSFCell classificationC = xssfRow.getCell(3);
String bookname=null;
String booktype=null;
String bookimg=null;
String bookdesc=null;
String classification=null;
if(booknameC !=null){
bookname = "'"+booknameC+"'";
}
if(booktypeC !=null){
booktype = "'"+booktypeC+"'";
}
if(bookdescC !=null){
bookdesc = "'"+bookdescC+"'";
}
if(classificationC !=null){
classification = "'"+classificationC+"'";
}
this.service.add(bookname, booktype, bookimg, bookdesc, classification);
}
}
}
}
}
}
} catch (Exception ex) {
request.setAttribute("message", "错误信息: " + ex.getMessage());
}
return "list/bookList";
}
}