package servlets;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
public class SCServlet extends HttpServlet {
private String uploadPath = "D:\\upload\\"; // 用于存放上传文件的目录
private String tempPath = "D:\\upload\\tmp\\"; // 用于存放临时文件的目录
public SCServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//当servlet收到浏览器发出的Post请求后,在doPost()方法中实现文件上传。以下是示例代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建新的文件
try {
DiskFileUpload fu=new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileitems=fu.parseRequest(request);
Iterator iterator=fileitems.iterator();
// 依次处理每一个文件:
while(iterator.hasNext()){
FileItem item=(FileItem) iterator.next();
// 获得文件名,这个文件名包括路径:
String fileName=item.getName();
if(fileName!=null) {
// 在这里可以记录用户和文件信息
// ...
// 写入文件a.txt,你也可以从fileName中提取文件名:
// item.write(new File(uploadPath + "a.txt"));
item.write(new File(uploadPath+"a.txt"));
}
// 跳转到上传成功提示页面
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 可以跳转出错页面
}
}
public void init() throws ServletException {
/*uploadPath = ....
tempPath = ....
// 文件夹不存在就自动创建:
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs();*/
}
}
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
public class SCServlet extends HttpServlet {
private String uploadPath = "D:\\upload\\"; // 用于存放上传文件的目录
private String tempPath = "D:\\upload\\tmp\\"; // 用于存放临时文件的目录
public SCServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//当servlet收到浏览器发出的Post请求后,在doPost()方法中实现文件上传。以下是示例代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建新的文件
try {
DiskFileUpload fu=new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileitems=fu.parseRequest(request);
Iterator iterator=fileitems.iterator();
// 依次处理每一个文件:
while(iterator.hasNext()){
FileItem item=(FileItem) iterator.next();
// 获得文件名,这个文件名包括路径:
String fileName=item.getName();
if(fileName!=null) {
// 在这里可以记录用户和文件信息
// ...
// 写入文件a.txt,你也可以从fileName中提取文件名:
// item.write(new File(uploadPath + "a.txt"));
item.write(new File(uploadPath+"a.txt"));
}
// 跳转到上传成功提示页面
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 可以跳转出错页面
}
}
public void init() throws ServletException {
/*uploadPath = ....
tempPath = ....
// 文件夹不存在就自动创建:
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs();*/
}
}