首页 新闻 搜索 专区 学院

struts2文件上传,items = upload.parseRequest(request);

0
悬赏园豆:15 [待解决问题]

各位圆友们好,我想请教一个问题,关于文件上传失败问题。

表单页面代码:

<form name="form1" method="post"
action="/servlet/ServletUpload?btURL=<s:property value="btURL" escape="false" />"
enctype="multipart/form-data">
<table width="300" border="0" class="table3">
<tr>
<td width="270" colspan="2"><input name="filen" type="file" accept="image/*" id="filen" size="35" maxlength="195" ></td>
</tr>
<tr>
<td valign="top">
</td>

<td valign="top" align="left">
<input type="button" name="Submit"
onClick="up();" value=" 上 传 ">

<script language="javascript">
function up(){
if(document.form1.filen.value!=null&&document.form1.filen.value!=""){
if(Check_FileType(document.form1.filen.value)){
document.form1.submit();
}
}else{
alert("请先选择上传文件");
return;
}
}
function Check_FileType(str){
var pos = str.lastIndexOf(".")+1;
var lastname = str.substring(pos,str.length); //此处文件后缀名也可用数组方式获得str.split(".")
if (lastname.toLowerCase()=="jpg"){
return true;
}else if (lastname.toLowerCase()=="jpeg"){
return true;
}else if (lastname.toLowerCase()=="gif"){
return true;
}else if (lastname.toLowerCase()=="png"){
return true;
}else if (lastname.toLowerCase()=="bmp"){
return true;
}else if (lastname.toLowerCase()=="JPG"){
return true;
}else if (lastname.toLowerCase()=="JPEG"){
return true;
}else if (lastname.toLowerCase()=="GIF"){
return true;
}else if (lastname.toLowerCase()=="rar"){
return true;
}else if (lastname.toLowerCase()=="RAR"){
return true;
}else if (lastname.toLowerCase()=="DOC"){
return true;
}else if (lastname.toLowerCase()=="doc"){
return true;
}else if (lastname.toLowerCase()=="BMP"){
return true;
}else if (lastname.toLowerCase()=="bmp"){
return true;
}else if (lastname.toLowerCase()=="BMP"){
return true;
}else if (lastname.toLowerCase()=="xls"){
return true;
}else if (lastname.toLowerCase()=="XLS"){
return true;
}else if (lastname.toLowerCase()=="xlsx"){
return true;
}else if (lastname.toLowerCase()=="XLSX"){
return true;
}else if (lastname.toLowerCase()=="wmv"){
return true;
}else if (lastname.toLowerCase()=="WMV"){
return true;
}else if (lastname.toLowerCase()=="wma"){
return true;
}else if (lastname.toLowerCase()=="WMA"){
return true;
}else if (lastname.toLowerCase()=="MP3"){
return true;
}else if (lastname.toLowerCase()=="mp3"){
return true;
}else if (lastname.toLowerCase()=="MP4"){
return true;
}else if (lastname.toLowerCase()=="mp4"){
return true;
}else if (lastname.toLowerCase()=="PDf"){
return true;
}else if (lastname.toLowerCase()=="pdf"){
return true;
}else if (lastname.toLowerCase()=="flv"){
return true;
}else if (lastname.toLowerCase()=="FLV"){
return true;
}else if (lastname.toLowerCase()=="TXT"){
return true;
}else if (lastname.toLowerCase()=="txt"){
return true;
}else{
alert("您上传的文件类型为"+lastname+",数据类型必须为图片类型");
document.form1.filen.focus();
return false;
}
}
</script>

 

后台代码:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String inputName =null;
String path =null;
response.setContentType("text/html; charset=gb2312");

PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
// ������������Ƿ�Ϊmultipart�ı?��ݡ�
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.print("�?��ݸ�ʽ����multipart/form-data�����߷Ƿ��ύ");
return;
}

//将请求消息实体中的每一个项目封装成单独的DiskFileItem (FileItem接口的实现) 对象的任务
//由 org.apache.commons.fileupload.FileItemFactory 接口的默认实现
DiskFileItemFactory factory = new DiskFileItemFactory();

// factory for
// disk-based
// file items
//factory.setSizeThreshold(1024000);//
File fileTemp = new File("c:\\temp");//创建一个文件,当文件项目大于临界值时使用的临时文件夹,默认采用系统默认的临时文件路径
fileTemp.mkdir(); //在已有的文件夹下创建

//用于配置在创建文件项目时,当文件项目大于临界值时使用的临时文件夹,默认采用系统默认的临时文件路径
factory.setRepository(fileTemp);

ServletFileUpload upload = new ServletFileUpload(factory);// �����ļ��ϴ�����
//upload.setSizeMax(1024000);// ���ÿͻ�������ϴ���-1Ϊ���޴󣨵�λ��byte��

// FileUploadBase.SizeLimitExceededException

List items = null;
List upItems=null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("�ϴ����ļ�����ϵͳ���ƣ�1MB��");
return;
}// ����request��������������List��

int itr = items.size();
String fName=null;
for (int i = 0;i<itr;i++) {// ѭ������?Ԫ��
FileItem item = (FileItem) items.get(i);

if (item.isFormField()) {// ��ͨ�ı?
if ("inputName".equals(item.getFieldName())) {
String name = item.getFieldName();
String value = item.getString("GB2312");
inputName = value;
// out.print(name + "��" + value);
}
if ("path".equals(item.getFieldName())) {
String name = item.getFieldName();
String value = item.getString("GB2312");
path = value;
// out.print(name + "��" + value);
}

}
}
String tpath;
tpath=getServletContext().getRealPath("/")+ path ;

File fileDir = new File(tpath);

if (!fileDir.exists()) {
fileDir.mkdirs();
}
tpath=fileDir.getPath();
for (int t = 0;t<itr;t++) {// ѭ������?Ԫ��
FileItem item = (FileItem) items.get(t);

if (item.isFormField()) {// ��ͨ�ı?

} else {// �ϴ��ļ�
if (item.getSize() != 0) {
String filename = item.getName();// ��ȡ�ļ��Ŀͻ����ϴ�·��
int i = filename.lastIndexOf(".");
Date d = new Date();
long dd =d.getTime();
filename = filename.substring(i + 1);// ��ȡ�ļ���
filename = dd+"."+filename;
File savedFile = new File(getServletContext().getRealPath(
"/")
+ path+"/", filename);
fName = path+"/"+filename;
try {
item.write(savedFile);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
HttpSession s =request.getSession();
// s.setAttribute("upload", "'"+inputName+"','"+fName+"'");

out.println(" </BODY>");
out.println("</HTML>");
// ServletContext sc = getServletContext();
// RequestDispatcher rd = sc.getRequestDispatcher("http://localhost/upload.do?inputName="+inputName+"&path="+path);
// rd.forward(request,response);
System.out.println("inputName---------------"+inputName);
System.out.println("fName---------------"+fName);
response.sendRedirect("/upload/upload.action?inputName="+inputName+"&upload="+fName);
out.flush();
out.close();

}

遇到的问题在这几行里

ServletFileUpload upload = new ServletFileUpload(factory);

List items = null;

items = upload.parseRequest(request);

当表单提交到该Servlet时,当图片内容大于2m,items里有值。

但是当图片内容小于2m的时候,items为null.

所以我的只能传大于或等于2m的文件,不知道为什么。

心静天宽净自由的主页 心静天宽净自由 | 初学一级 | 园豆:187
提问于:2015-04-07 11:34
< >
分享
所有回答(1)
0

看看ServletFileUpload是怎么实现的呗。

Firen | 园豆:5483 (大侠五级) | 2015-04-08 18:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册