struts的上传和下载
上传: jsp: <body> <h1>filogin</h1> <!--如果表单中有文件文件控件,上传的编码必须是multipart/form-data --> <form action="/strutsFileUpAndDown/login.do" method="post" enctype="multipart/form-data"> name<input id="text" type="text" name="name"/><br/> file<input id="formFile" type="file" name="formFile"/><br/> <input type="submit" value="注册"/><br/> <input type="button" value="1" onclick="test();"/> </form> </body> form: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.zh.struts.form; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; /** * MyEclipse Struts * Creation date: 11-11-2013 * * XDoclet definition: * @struts.form name="userForm" */ public class UserForm extends ActionForm { private String name; private FormFile formFile; public String getName() { return name; } public FormFile getFormFile() { return formFile; } public void setName(String name) { this.name = name; } public void setFormFile(FormFile formFile) { this.formFile = formFile; } } action: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.zh.struts.action; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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 com.zh.domain.UserName; import com.zh.service.UserNameService; import com.zh.struts.form.UserForm; import com.zh.utils.Tools; /** * MyEclipse Struts Creation date: 11-11-2013 * * XDoclet definition: * * @struts.action path="/login" name="userForm" scope="request" validate="true" */ public class LoginAction extends Action { /* * Generated Methods */ /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub String name = userForm.getName(); String fileName = userForm.getFormFile().getFileName(); String filePath = request.getServletContext().getRealPath("/file"); // 要判断文件是否有 . if (fileName.indexOf(".") == -1) { // 这里还要返回一个信息回去。 return mapping.findForward("gotoErr"); } String newFileName = Tools.getFileName(fileName); filePath += "\\" + newFileName; UserName domainUserName = new UserName(); domainUserName.setName(name); domainUserName.setNewPhoName(newFileName); domainUserName.setOldPhoName(fileName); // 调用 UserNameService的函数 UserNameService userNameService = new UserNameService(); if (userNameService.addUser(domainUserName)) { // 把数据读入文件夹 InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = userForm.getFormFile().getInputStream(); outputStream = new FileOutputStream(filePath); // 做一个缓冲 int len = 0; byte[] bytes = new byte[1024]; while ((len = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } return mapping.findForward("main"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { outputStream.close(); inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { return mapping.findForward("gotoErr"); } return null; } } 下载: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.zh.struts.action; import java.io.*; import java.util.LinkedList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.*; import com.zh.domain.UserName; import com.zh.service.UserNameService; /** * MyEclipse Struts * Creation date: 11-12-2013 * * XDoclet definition: * @struts.action validate="true" */ public class GotoDownAction extends Action { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub String name=request.getParameter("name"); UserNameService userNameService=new UserNameService(); UserName userName=userNameService.getZhuCeList(name); //取出数据的路径 String file=request.getServletContext().getRealPath("/file"); file+="\\"+userName.getNewPhoName(); //设置要下载的信息 String fileOldName=userName.getOldPhoName(); //这里是给中文名字的文件编码 try { fileOldName=java.net.URLEncoder.encode(fileOldName,"utf-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //设置响应的信息 response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment; filename="+fileOldName); //传送数据 InputStream inputStream=null; OutputStream outputStream=null; try { response.setContentType("application/x-download"); inputStream=new FileInputStream(file); outputStream=response.getOutputStream(); byte[] buffer=new byte[1024]; int len=0; while((len=inputStream.read(buffer))!=-1){ outputStream.write(buffer, 0, len); } //注意这里千万不要跳转,会出错 getOutputStream() has already been called for this response //return mapping.findForward("gotoList"); return null; } catch (Exception e) { // TODO Auto-generated catch blocke //这里是调到 全局Action return mapping.findForward("gotoErr"); //e.printStackTrace(); }finally{ try { outputStream.close(); inputStream.close(); } catch (Exception e) { System.out.println("e"); // TODO Auto-generated catch block e.printStackTrace(); } } } }