FileUploadServlet
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.FileItem;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.MultipartStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.windfone.common.FileTool;
/**
* 文件上传的Servlet,我们使用Apache组织提供的commons-fileupload.jar和commons-io.jar包实现文件上传功能
* @author Administrator
*
*/
public class FileUploadServlet extends HttpServlet {
/** Constructor of the object. */
public FileUploadServlet() {
super();
}
/** Destruction of the servlet. */
public void destroy() {
super.destroy();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
OutputStream os = null;
FileInputStream is = null;
try {
UploadBean fileBean = new UploadBean();
String tmpFile = this.getServletContext().getRealPath("/");
File file = new File(tmpFile + "temp");
if (!file.exists()) {
file.mkdirs();
}
fileBean.setObjectPath(tmpFile + "temp"+File.separator);
fileBean.setSourceFile(request);
String uploadFolder =fileBean.getFieldValue("uploadFolder");
String fileName = fileBean.getObjectFileName()[0];
String root = tmpFile+"upload"+File.separator + uploadFolder+File.separator;
File tmp = new File(root);
if ( !tmp.exists())
tmp.mkdirs();
root = FileTool.getFolderPath(root, 0,false);
File diskFile = new File(root,fileName );
String returnPic = diskFile.getAbsolutePath();
int begin = returnPic.indexOf("upload");
returnPic = returnPic.substring(begin, returnPic.length() );
returnPic = returnPic.replace("//", "/");
File inputFile = new File(tmpFile + "temp"+File.separator +fileName);
is = new FileInputStream(inputFile);
os = new FileOutputStream(diskFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
if (inputFile.exists())
inputFile.delete();
out.println("<script>parent.callback('upload file success|"+ returnPic +"')</script>");
}catch(Exception e){
e.printStackTrace();
out.println("<script>parent.callback('upload file error')</script>");
}finally {
if ( os !=null )
os.close();
if ( is !=null )
is.close();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
}
}
UploadBean
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
/**
*
*
*
* Title: 文件上传类
*
* Description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件
*
* Copyright: Copyright (c) 2008
*
* @version 1.0
*
*/
public class UploadBean {
private String[] sourceFile = new String[255]; // 源文件名
private String[] suffix = new String[255]; // 文件后缀名
private String canSuffix = ".gif.jpg.jpeg.png"; // 可上传的文件后缀名
private String objectPath = "c:/"; // 目标文件目录
private String[] objectFileName = new String[255]; // 目标文件名
private ServletInputStream sis = null; // 输入流
private String[] description = new String[255]; // 描述状态
private long size = 1024 * 1024 * 5; // 限制大小
private int count = 0; // 已传输文件数目
private byte[] b = new byte[4096]; // 字节流存放数组
private boolean successful = true;
private Hashtable fields = new Hashtable();
public UploadBean() {
}
// 设置上传文件的后缀名
public void setSuffix(String canSuffix) {
this.canSuffix = canSuffix;
}
// 设置文件保存路径
public void setObjectPath(String objectPath) {
this.objectPath = objectPath;
}
// 设置文件保存路径
public void setSize(long maxSize) {
this.size = maxSize;
}
// 文件上传处理程序
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("filename=/"")) != -1) {
// 取得文件数据
s = s.substring(k + 10);
k = s.indexOf("/"");
s = s.substring(0, k);
sourceFile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k + 1);
if (canTransfer(count)) {
transferFile(count);
}
++count;
} else if ((k = s.indexOf("name=/"")) != -1) {
// 普通表单输入元素,获取输入元素名字
String fieldName = s.substring(k + 6, s.length() - 3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a - 2);
if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
// 取得表单元素值
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
// 取得上传文件数
public int getCount() {
return count;
}
// 取得目标路径
public String getObjectPath() {
return objectPath;
}
// 取得源文件名
public String[] getSourceFile() {
return sourceFile;
}
// 取得目标文件名
public String[] getObjectFileName() {
return objectFileName;
}
// 取得上传状态描述
public String[] getDescription() {
return description;
}
// 判断上传文件的类型
private boolean canTransfer(int i) {
// suffix[i] = suffix[i].toLowerCase();
// // 这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件
// if (sourceFile[i].equals("") || (!(canSuffix.indexOf("." + suffix[i]) >= 0))) {
// description[i] = "ERR: File suffix is wrong.";
// return false;
// } else {
// }
return true;
}
// 上传文件转换
private void transferFile(int i) {
String x = Long.toString(new java.util.Date().getTime());
try {
objectFileName[i] = x + "." + suffix[i];
FileOutputStream out = new FileOutputStream(objectPath+ objectFileName[i]);
int a = 0;
int k = 0;
long hastransfered = 0; // 标示已经传输的字节数
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45)&& (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[count] = "ERR: The file "+ sourceFile[count]+ " is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description[count] = "Right: The file " + sourceFile[count]
+ " has been transfered successfully.";
}
out.close();
if (!successful) {
sis.close();
File tmp = new File(objectPath + objectFileName[count]);
tmp.delete();
}
} catch (IOException ioe) {
description[i] = ioe.toString();
}
}
}