这里用到了一个开源的包commons-fileupload.jar,事实证明这个包十分好用
uploadPhoto.jsp:
<%
@ page contentType
=
"
text/html; charset=GBK
"
%>
< html >
< head >
< title >
upload
</ title >
</ head >
< body bgcolor = " #ffffff " >
< h1 >
<%
if (request.getAttribute( " msg " ) == null )
{//提示信息
} else {
%>
<%=(String)request.getAttribute("msg")%>
<%
}
%>
</ h1 >
< form action = " photoImg.jsp " id = " frm " encType = " multipart/form-data " method = " post " >
< br />< br />
< input type = " FILE " name = " FILE1 " id = " myFilename " size = " 50 " />
< input type = " submit " name = " Submit " value = " Submit " />
< input type = " reset " value = " Reset " />
</ form >
</ body >
</ html >
< html >
< head >
< title >
upload
</ title >
</ head >
< body bgcolor = " #ffffff " >
< h1 >
<%
if (request.getAttribute( " msg " ) == null )
{//提示信息
} else {
%>
<%=(String)request.getAttribute("msg")%>
<%
}
%>
</ h1 >
< form action = " photoImg.jsp " id = " frm " encType = " multipart/form-data " method = " post " >
< br />< br />
< input type = " FILE " name = " FILE1 " id = " myFilename " size = " 50 " />
< input type = " submit " name = " Submit " value = " Submit " />
< input type = " reset " value = " Reset " />
</ form >
</ body >
</ html >
注意:这个form必须写上encType="multipart/form-data" 来表明上传文件。并且,这个form中不能用隐藏变量来传递参数。就是说写一个<input type = hidden name=tmp>在下一个页面或者action中用request.getParameter是取不到的。
photoImg.jsp:
uploadImage()是这样实现的:
public
static
HttpServletRequest uploadImage(HttpServletRequest request)
throws Exception {
DiskFileUpload fileUpload = new DiskFileUpload();
//设置允许用户上传文件大小,单位:字节
fileUpload.setSizeMax(8388608);
//设置最多允许在内存中存储的数据,单位:字节
fileUpload.setSizeThreshold(1024000);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//在进行文件上传的时候文件先存再内存中,然后才会存到server上,但是如果内存放不下那么大的文件
//就必须用硬盘上的 一个临时文件夹来保存这个文件的部分,然后转存
//现在默认的文件存储的路径是
String tmp = “c:/temp/”;
//设置上传文件的存储路径,如果不设置这个路径,这个文件将保存在
//server的根目录下(如tomcat或者resin的根目录)
String uploadPath = “c:/”;
fileUpload.setRepositoryPath(tmp);
String strType = "";
try {
List fileItems = fileUpload.parseRequest(request);
Iterator iterator = fileItems.iterator();
while(iterator.hasNext()) {
FileItem fileItem = (FileItem)iterator.next();
//文件域的表单信息
if (!fileItem.isFormField()) {
String strName = fileItem.getName();
long size = fileItem.getSize();
if((strName==null||strName.equals("")) && size==0)
continue;
File savedFile = new File(uploadPath + strName);
fileItem.write(savedFile);
request.setAttribute("msg","save file successful!");
}
}
if(request.getAttribute("msg")== null)
{
request.setAttribute("msg","save file failed!");
}
} catch (Exception ex) {
request.setAttribute("msg","save file failed!");
}
return request;
}
throws Exception {
DiskFileUpload fileUpload = new DiskFileUpload();
//设置允许用户上传文件大小,单位:字节
fileUpload.setSizeMax(8388608);
//设置最多允许在内存中存储的数据,单位:字节
fileUpload.setSizeThreshold(1024000);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//在进行文件上传的时候文件先存再内存中,然后才会存到server上,但是如果内存放不下那么大的文件
//就必须用硬盘上的 一个临时文件夹来保存这个文件的部分,然后转存
//现在默认的文件存储的路径是
String tmp = “c:/temp/”;
//设置上传文件的存储路径,如果不设置这个路径,这个文件将保存在
//server的根目录下(如tomcat或者resin的根目录)
String uploadPath = “c:/”;
fileUpload.setRepositoryPath(tmp);
String strType = "";
try {
List fileItems = fileUpload.parseRequest(request);
Iterator iterator = fileItems.iterator();
while(iterator.hasNext()) {
FileItem fileItem = (FileItem)iterator.next();
//文件域的表单信息
if (!fileItem.isFormField()) {
String strName = fileItem.getName();
long size = fileItem.getSize();
if((strName==null||strName.equals("")) && size==0)
continue;
File savedFile = new File(uploadPath + strName);
fileItem.write(savedFile);
request.setAttribute("msg","save file successful!");
}
}
if(request.getAttribute("msg")== null)
{
request.setAttribute("msg","save file failed!");
}
} catch (Exception ex) {
request.setAttribute("msg","save file failed!");
}
return request;
}
OK,文件保存了
我在 windows 下传文件很正常,但是在 linux 下,当文件过大,需要使用临时文件夹的时候出现问题,可能是文件路径的问题。
<%
@ page contentType
=
"
text/html; charset=GBK
"
%>
<% @ page import = " java.util.* " %>
<% @ page import = " java.awt.* " %>
< html >
< head >
< title >
upload
</ title >
</ head >
< body bgcolor = " #ffffff " >
<%
GPhoto photo = new GPhoto();
request = photo.uploadImage(request);
%>
< jsp:forward page = " uploadPhoto.jsp " >
</ jsp:forward >
</ body >
</ html >
<% @ page import = " java.util.* " %>
<% @ page import = " java.awt.* " %>
< html >
< head >
< title >
upload
</ title >
</ head >
< body bgcolor = " #ffffff " >
<%
GPhoto photo = new GPhoto();
request = photo.uploadImage(request);
%>
< jsp:forward page = " uploadPhoto.jsp " >
</ jsp:forward >
</ body >
</ html >