<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadException"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="com.khnt.cdsp.xzxk.Import"%>
<%@ page import="com.khnt.pub.MemsUser" %>
<%@ page import="com.khnt.frame.foundation.user.User" %>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
String resultMsg = "";
String filePath = "";
MemsUser user = (MemsUser) session.getAttribute(User.NAME);
String aCode = user.getAreacode();
String paths = request.getParameter("path");
String path = pageContext.getServletContext().getRealPath("/") + "files/" + paths + "/";
try
{
// 1. 上传文件
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4*1024);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000*1024*1024);
List itemList = null;
try {
itemList = upload.parseRequest(request);
}catch (FileUploadBase.SizeLimitExceededException e){
// 请求数据的size超出了规定的大小.
e.printStackTrace();
response.getWriter().print("{success:false,errors:'请求数据的size超出了规定的大小'}");
return;
}catch (FileUploadBase.InvalidContentTypeException e){
// 无效的请求类型,即请求类型enctype != "multipart/form-data"
response.getWriter().print("{success:false,errors:'请求类型enctype != multipart/form-data'}");
return;
}catch (FileUploadException e){
// 如果都不是以上子异常,则抛出此总的异常,出现此异常原因无法说明.
response.getWriter().print("{success:false,errors:'上传过程异常,导致其原因可能是网速过慢或者其它原因'}");
return;
}
if(itemList != null){
Iterator it = itemList.iterator();
while(it.hasNext())
{
FileItem item =(FileItem) it.next();
if(!item.isFormField())
{
String totalName = item.getName();
String name = "temp";
if(totalName != ""){
int index = totalName.lastIndexOf("\\");
name = totalName.substring(index+1);
//System.out.println(name);
}else{
name = "temp";
}
try {
filePath = path + name;
//System.out.println("-------------------" + path +name);
item.write(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("{success:false,errors:'写入磁盘错误'}");
return;
}
}
}
}
else{
response.getWriter().print("{success:false,errors:'上传数据空'}");
return;
}
// 2. 导入数据(操作数据库)
resultMsg = Import.impExcel(filePath,aCode,user.getUserId());
// 3. 返回结果
out.print(resultMsg);
//response.getWriter().print("{success:true,msg:'导入成功!'}");
}
catch (Exception e)
{
e.printStackTrace();
out.print(resultMsg);
//response.getWriter().print("{success:false,errors:'导入失败,请检查文件格式是否正确'}");
}
%>
jsp中文件上传的实现
原创mb64df323cb1db4 ©著作权
©著作权归作者所有:来自51CTO博客作者mb64df323cb1db4的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:JAR命令打包&TLD文件
下一篇:JSTL笔记-c标签

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
jsp中文件下载的实现
式进行;2、采用文件流输出的方式下载。 1、采用RequestDispatcher的方式
jsp exception null string byte -
上传文件的jsp文件代码,uploadfile.jsp
正在学习struts这个框架,目前正在利用这个框架开发一个小型的Blog Web,其中涉
JSP Struts Apache Servlet Myeclipse