dinux 2011-08-19
package com.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
import java.util.Map;import javax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jivesoftware.openfire.plugin.db.DbManager;/******************************************************************
**文件名:Up.java
**Copyright(c)[email protected]
**创建人:[email protected]
**日期:2011-05-04
**修改人:
**日期:
**描述:uploadServlet
**jsppage<formaction="u"method="post"enctype="multipart/form-data">
**版本:
**
**==============================================================
**
**修改记录
**
**版本号修改编号修改者修改日期修改说明
**===========================================
**0001
**0002
**
********************************************************************/
publicclassUpextendsHttpServlet{
private static final long serialVersionUID = 1L;public void init(ServletConfig config) throws ServletException {
super.init(config);
}public void destroy() {
super.destroy();
}private void up(HttpServletRequest request, HttpServletResponse response)
throwsServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriterout=response.getWriter();
Stringp="",filename=null;
Mapmap=newHashMap();
byte[]bytes=null;
try {// 文件大小限制10M
intmaxPostSize=10*1024*1024;
//文件工厂类
DiskFileItemFactoryfactory=newDiskFileItemFactory();
//factory.setSizeThreshold(4096);
ServletFileUploadupload=newServletFileUpload(factory);
//upload.setHeaderEncoding("UTF-8");
//upload.setSizeMax(maxPostSize);
ListfileItems=upload.parseRequest(request);
if(fileItems!=null&&fileItems.size()>0){
Iteratoriter=fileItems.iterator();
while(iter.hasNext()){
FileItemitem=(FileItem)iter.next();
if(item.isFormField()){
if(!"submit".equals(item.getFieldName())){
p=item.getString("UTF-8");
System.out.println(item.getFieldName() + " :" + p);map.put(item.getFieldName(), p);
}
} else {bytes = item.get();
if(bytes.length>0){
filename=item.getName();
System.out.println(filename);
//item.write(newFile("d:/"+filename));
}
}
}
}
if(bytes!=null&&bytes.length>0)
DbManager.getInstance().saveFile(bytes, filename, map);out.write("ok!!!" + p);
} catch (Exception e) {
e.printStackTrace();
out.write("Exception!!!");
} finally {}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throwsServletException,IOException{
doPost(req,resp);
}protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throwsServletException,IOException{
req.setCharacterEncoding("UTF-8");
up(req,resp);
}/*
* ------这个是后台用jsp实现
*
*<%DiskFileItemFactoryfactory=newDiskFileItemFactory();
*ServletFileUploadupload=newServletFileUpload(factory);try{List
*items=upload.parseRequest(request);Iteratoritr=items.iterator();
*while(itr.hasNext()){FileItemitem=(FileItem)itr.next();if
*(item.isFormField()){System.out.println("表单参数名:"+item.getFieldName()
*+",表单参数值:"+item.getString("UTF-8"));}else
*{System.out.println("上传文件的大小:"+item.getSize());
*System.out.println("上传文件的类型:"+item.getContentType());
*System.out.println("上传文件的名称:"+item.getName());FiletempFile=new
*File(item.getName());Filefile=newFile(request.getRealPath("/")+
*"photofiles",tempFile.getName());item.write(file);Stringfname=
*item.getName();%><scriptlanguage="javascript">alert("上传成功!");
*</script><%}}}catch(Exceptione){e.printStackTrace();%><script
*language="javascript">alert("上传失败,请重新上传!");</script><%}
*/}