struts图片上传

来源:互联网 发布:linux vps python 编辑:程序博客网 时间:2024/05/02 02:48
简单的struts上传的:
jsp:
<body>
  <html:form action="/upload" method="POST" enctype="multipart/form-data"><!-- 注意编码 -->
  <html:file property="theFile"></html:file><br>
  <html:submit>提交</html:submit>
  </html:form>
  </body>
form:
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
public class UploadForm extends ActionForm{
    private FormFile theFile;
    public void reset(ActionMapping mapping,HttpServletRequest request){
     this.theFile=null;
    }
    public void setTheFile(FormFile theFile){
     this.theFile=theFile;
    }
    public FormFile getTheFile(){
     return this.theFile;
    }
}
action:
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.struts.form.UploadForm;
import java.io.FileOutputStream;
import java.io.File;
/*
 * 测试struts的上传Action
 */
public class TestUpLoadAction extends Action{
      public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
       UploadForm uf=(UploadForm)form;
       FormFile ff=uf.getTheFile();
       HttpSession session=request.getSession();
       String fileName=ff.getFileName();//获取名称
       int size=ff.getFileSize();
       String contentType=ff.getContentType();//获取类型
       byte[] data=ff.getFileData();//获取上传内容
       session.setAttribute("fileName",fileName);
       session.setAttribute("contentType",contentType);
       session.setAttribute("size",size);
       FileOutputStream fo=new FileOutputStream(new File("e:\\"+fileName));//你要保存的地址
       fo.write(data);
       fo.close();
      return mapping.findForward("download");
      }
}

     <form-bean name="uploadform" type="com.struts.form.UploadForm"></form-bean>

   <action type="com.struts.action.TestUpLoadAction" path="/upload" name="uploadform">
      <forward name="download" path="/TestStrutsDownload.jsp"></forward>
      </action>

0 0
原创粉丝点击