package struts.action;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import comm.Page;
import services.ImageServices;
import tools.SpringUnit;
import bean.ImageBean;
public class UpLoadAction extends Action {
private PrintWriter out = null;
private ImageServices services=null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
services=(ImageServices) SpringUnit.getBean("imageServicesImpl");
out=response.getWriter();
String action=request.getParameter("action").trim();
if(action.equalsIgnoreCase("addpic")){
addPic(request,response);
}
else if(action.equalsIgnoreCase("listPic")){
listPic(request,response);
}else if(action.equalsIgnoreCase("delPic")){
delPic(request,response);
}
return null;
}
private void delPic(HttpServletRequest request, HttpServletResponse response) {
int id=-1;
try{
id=Integer.parseInt(request.getParameter("id").trim());
}catch (Exception e) {
id=-1;
}
services.deleImg(id);
listPic(request, response);
}
private void listPic(HttpServletRequest request,
HttpServletResponse response) {
response.reset();
response.setCharacterEncoding("GBK");
response.setContentType("application/xml");
Document doc = new Document();
Page page=page(request, response);
List<ImageBean> lst=page.getDispList();
Element root= new Element("root");
Element element= new Element("msg");
element.setAttribute("pageId",page.getPageId()+"");
element.setAttribute("pageCount",page.getPageCount()+"");
element.setAttribute("pageSize",page.getPageSize()+"");
root.addContent(element);
for(ImageBean bean:lst){
if(!(bean.getBys()==null)){
element= new Element("listes");
element.setAttribute("id",bean.getId()==0?"no":bean.getId()+"");
element.setAttribute("url",bean.getBys()==null?"no":"servlet/ShowPic?id="+bean.getId());
root.addContent(element);
}
}
doc.addContent(root);
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()
.setEncoding("iso-8859-1"));
try {
outputter.output(doc, out);
} catch (Exception ex) {
}
}
private Page page(HttpServletRequest request,HttpServletResponse response){
String pageId;
try{
pageId=request.getParameter("pageId").trim();
if(pageId==null||pageId.equalsIgnoreCase("")){
pageId="0";
}
}catch (Exception e) {
pageId="0";
}
Page page=new Page(-1,services.listImg());
page.computePage(pageId);
return page;
}
private void addPic(HttpServletRequest request, HttpServletResponse response) {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload file=new ServletFileUpload(factory);
try {
List lst=file.parseRequest(request);
Iterator it=lst.iterator();
while(it.hasNext()){
FileItem fileItem=(FileItem)it.next();
if(fileItem.isFormField()){
}
else{
byte buf[]=fileItem.get();
ImageBean bean=new ImageBean();
bean.setBys(buf);
services.addImg(bean);
request.setAttribute("upLoad message","上传成功");
}} }catch (Exception e) {
request.setAttribute("upLoad message","上传失败");
e.printStackTrace();
}
}
}