FileUploadServlet 文件上传

package com.appdev.bsf.server.servlet;

importjava.io.File;

importjava.io.IOException;

importjava.util.Date;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.List;

importjava.util.Map;

importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.apache.commons.fileupload.FileItem;

importorg.apache.commons.fileupload.FileItemFactory;

importorg.apache.commons.fileupload.FileUploadException;

importorg.apache.commons.fileupload.disk.DiskFileItemFactory;

importorg.apache.commons.fileupload.servlet.ServletFileUpload;

importorg.springframework.web.context.WebApplicationContext;

importorg.springframework.web.context.support.WebApplicationContextUtils;

importcom.appdev.bsf.common.client.datasource.DictionaryGwtRpcService;

importcom.ibm.icu.text.SimpleDateFormat;

publicclassFileUploadServletextendsHttpServlet{

privatestaticfinallongserialVersionUID=1L;

privateDictionaryGwtRpcServiceservice;

privateServletFileUploadupload;

privateStringyear;

privateStringmonth;

privateStringday;

@Override

publicvoidinit()throwsServletException{

FileItemFactoryfactory=newDiskFileItemFactory();

upload=newServletFileUpload(factory);

ServletContextapplication;

WebApplicationContextwac;

application=getServletContext();

wac=WebApplicationContextUtils.getWebApplicationContext(application);

service=(DictionaryGwtRpcService)wac.getBean("dictionaryGwtRpcServiceImpl");

Datedate=newDate();

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");

String[]dateArr=sdf.format(date).split("-");

year=dateArr[0];

month=dateArr[1];

day=dateArr[2];

}

@Override

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,

IOException{

response.setContentType("text/html;charset=utf-8");

Integerid=null;

StringclsUrl=null;

Map<String,String>data=null;

try{

id=Integer.valueOf(request.getParameter("id"));

clsUrl=request.getParameter("clsUrl");

data=newHashMap<String,String>();

}catch(Exceptione){

e.printStackTrace();

}

List<?>items=null;

upload.setSizeMax(1024*1024*2);

try{

items=upload.parseRequest(request);

}catch(FileUploadExceptionex){

thrownewServletException(ex);

}

Iterator<?>iter=items.iterator();

while(iter.hasNext()){

FileItemitem=(FileItem)iter.next();

if(item.isFormField()==false){

operatorFile(item,id,clsUrl,data);

}

}

}

privatevoidoperatorFile(FileItemitem,Integerid,StringclsUrl,Map<String,String>data){

StringfileName=null;

//获得上传路径

Stringpath="/uploads/"+year+"/"+month+"/"+day;

StringsavePath=getServletContext().getRealPath(path);

//获得文件扩展名

Stringext=item.getName().substring(item.getName().lastIndexOf("."));

//取的file域的值的名字,不带路径

intpos=item.getName().lastIndexOf("\\");

StringfieldValue=item.getName().substring(pos+1);

//或直接保存成文件

//判断当天的文件夹是否存在,按年/月/日创建,如果不存在则创建

if(!newFile(savePath).isDirectory()){

newFile(savePath).mkdirs();

}

//判断是否有扩展名exe

if("exe".equals(ext)){

return;

}

Filefile=newFile(savePath,fieldValue);

try{

fileName=item.getFieldName();

//写文件

item.write(file);

//存路径

data.put(fileName,path+"/"+item.getName());

//数据库写值

service.updateFiledUrl(clsUrl,id,data);

}catch(Exceptione){

e.printStackTrace();

}//直接保存文件

}

}

相关推荐