我用的fileUpload的1.0版本
最新版本没有使用
自己配置web.xml调用
只实现了一些基本的操作~~~
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
public class Upload extends HttpServlet {
private String uploadPath = "C://upload//"; // 上传文件的目录
private String tempPath = "C://upload//tmp//"; // 临时文件目录
//在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while(i.hasNext()) {
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
if(fi.getName()!=null){
System.out.print("=============Test===============<br/>");
String fileName = fi.getName();
System.out.print("The file Name is :"+fileName+"<br/>");
System.out.print("The content type is :"+fi.getContentType()+"<br/>");
System.out.print("The fieldName is "+fi.getFieldName()+"<br/>");
System.out.print("The file Size is "+fi.getSize()+"<br/>");
//out.print("The file string is "+fi.getString()+"<br/>"); //显示文件按得内容
//获得文件名后缀
int index = fileName.lastIndexOf('.');
String suffix = null;
if(index != -1)
{
suffix = fileName.substring(index + 1, fileName.length());
}
System.out.print("the suffix is :"+suffix+"<br/>");
//生成新文件名
String newFileName = /*uploadPath +*/ String.valueOf((new java.util.Date()).getTime())+"."+suffix;
System.out.print("the new file name is :"+newFileName+"<br/>");
// 在这里可以记录用户和文件信息
// ...
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
if(fi.getSize()>500000){
System.out.print("文件太大,上传失败!<br/>");
}else{
fi.write(new File(uploadPath + newFileName));//"a.txt"));
}
}
}
}
catch(Exception e) {
// 可以跳转出错页面
}
}
//如果要在配置文件中读取指定的上传文件夹,可以在init()方法中执行:
public void init() throws ServletException {
uploadPath = "d://upload//";
tempPath = "d://upload//tmp//";
// 文件夹不存在就自动创建:
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs();
}
}
最新版本没有使用
自己配置web.xml调用
只实现了一些基本的操作~~~
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
public class Upload extends HttpServlet {
private String uploadPath = "C://upload//"; // 上传文件的目录
private String tempPath = "C://upload//tmp//"; // 临时文件目录
//在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while(i.hasNext()) {
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
if(fi.getName()!=null){
System.out.print("=============Test===============<br/>");
String fileName = fi.getName();
System.out.print("The file Name is :"+fileName+"<br/>");
System.out.print("The content type is :"+fi.getContentType()+"<br/>");
System.out.print("The fieldName is "+fi.getFieldName()+"<br/>");
System.out.print("The file Size is "+fi.getSize()+"<br/>");
//out.print("The file string is "+fi.getString()+"<br/>"); //显示文件按得内容
//获得文件名后缀
int index = fileName.lastIndexOf('.');
String suffix = null;
if(index != -1)
{
suffix = fileName.substring(index + 1, fileName.length());
}
System.out.print("the suffix is :"+suffix+"<br/>");
//生成新文件名
String newFileName = /*uploadPath +*/ String.valueOf((new java.util.Date()).getTime())+"."+suffix;
System.out.print("the new file name is :"+newFileName+"<br/>");
// 在这里可以记录用户和文件信息
// ...
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
if(fi.getSize()>500000){
System.out.print("文件太大,上传失败!<br/>");
}else{
fi.write(new File(uploadPath + newFileName));//"a.txt"));
}
}
}
}
catch(Exception e) {
// 可以跳转出错页面
}
}
//如果要在配置文件中读取指定的上传文件夹,可以在init()方法中执行:
public void init() throws ServletException {
uploadPath = "d://upload//";
tempPath = "d://upload//tmp//";
// 文件夹不存在就自动创建:
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs();
}
}