丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說 明 你 沒 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1
          JSP頁面
           <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
          <html>
           <head>
            <title>JSP for FileForm form</title>
           </head>
           <body>
            <html:form action="/file" enctype="multipart/form-data">
             上傳 : <html:file property="myFile"/><html:errors name="fileError"/>
             <html:submit value="上傳"/>
            </html:form>
            <html:img src="image/dd.jpg"/>
           </body>
          </html>

          /*******************************************************************************/
          對應的ActionForm
          package wsq.struts.form;

          import javax.servlet.http.HttpServletRequest;
          import org.apache.struts.action.ActionErrors;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.upload.FormFile;

          /**
           * MyEclipse Struts
           * Creation date: 09-17-2007
           *
           *   @author 王世清
           **/
          public class FileForm extends ActionForm {
           
           private static final long serialVersionUID = 1L;

           private FormFile myFile;

           public ActionErrors validate(ActionMapping mapping,
             HttpServletRequest request) {
            return null;
           }

           public void reset(ActionMapping mapping, HttpServletRequest request) {
            
           }

           
           public static long getSerialVersionUID() {
            return serialVersionUID;
           }

           public FormFile getMyFile() {
            return myFile;
           }

           public void setMyFile(FormFile myFile) {
            this.myFile = myFile;
           }

           

           
          }


           

          /**********************************************************/
          對應的Action


          package wsq.struts.action;
          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 org.apache.struts.upload.FormFile;

          import wsq.struts.form.FileForm;

          public class FileAction extends Action {
             

           public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response)  throws IOException{
            FileForm fileForm = (FileForm) form;
            
            FormFile file = fileForm.getMyFile();
            if (file == null)
            {  
             return mapping.findForward("error");
            }
           String dir=servlet.getServletContext().getRealPath("/image");//首先在你的項目下新建個(在 webroot下)image文件夾
          /***注釋的是按孫衛琴書上來的*************/
          //  InputStream in=file.getInputStream();
          //  OutputStream out=new FileOutputStream(dir+"/"+file.getFileName());
          //  int bytesRead=0;
          //  byte [] buffer=new byte[8000];
          //  while((bytesRead=in.read(buffer, 0, 8000))!=-1)
          //  {
          //   out.write(buffer,0,bytesRead);
          //   
          //  }
            
            FileOutputStream out = new FileOutputStream(dir+"/" + file.getFileName());
            out.write(file.getFileData());

            out.close();
            //in.close();
            file.destroy();
           
            System.out.println(file.getFileName()+"上傳文件的名字");
            return null;
           }
          }




          /*************************************/
          大家還要記得傳圖片是會出現文件名亂碼 解決的辦法是寫個過濾器
            要是不想寫可以在
                         你 的TOMCATE目錄下的\webapps\examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.java
          TOMCAT也為你寫好一個現成的  拿來就可以用了
          這兩天嘗試 用AJAX來寫一個文件上傳的  呵呵。。。




          posted on 2007-09-20 12:06 Crying 閱讀(833) 評論(2)  編輯  收藏 所屬分類: Jsp+Struts

          FeedBack:
          # re: struts中的文件上傳
          2007-10-11 16:03 | Crying
          下載

          public ActionForward download(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {
          if (logger.isDebugEnabled()) {
          logger.debug("entering 'AttachmentAction.download()' method...");
          }
          ActionMessages messages = new ActionMessages();

          String id = request.getParameter("id");
          String attachmentFile=request.getParameter("file");
          String type = request.getParameter("type");
          if (id != null||attachmentFile!=null) {
          Attachment attachment =null;
          if(id!=null) {
          attachment= mgr.view(id);
          } else if(attachmentFile!=null) {
          attachment=mgr.viewByFile(attachmentFile);
          }

          if (attachment == null) {
          messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
          "object.miss"));
          saveMessages(request, messages);
          return mapping.findForward("failure");
          }
          //filename=new String(filename.getBytes("iso-8859-1"),"gb2312");
          //String filepath=this.getServletContext().getRealPath("/upload");
          File file = new File(mgr.getRoot()+"/"+attachment.getAttachmentFile());
          String fileName = URLEncoder.encode(attachment.getAttachment(),
          "UTF-8");
          BufferedInputStream br = new BufferedInputStream(
          new FileInputStream(file));
          byte[] buf = new byte[1024 * 1024];
          int len = 0;
          response.reset(); //純下載方式
          //response.setContentType("application/x-msdownload");
          if (type == null) {
          response
          .setContentType("application/octet-stream;charset=utf-8");
          response.setCharacterEncoding("UTF-8");
          response.setHeader("Content-Disposition",
          "attachment; filename=" + fileName);
          } else if (type != null && type.equals("jpg")) {
          response.setHeader("Cache-Control", "no-store");
          response.setDateHeader("Expires", 0);
          response.setContentType("image/jpeg");
          }
          OutputStream out = response.getOutputStream();
          while ((len = br.read(buf)) > 0)
          out.write(buf, 0, len);
          br.close();
          out.close();
          }
          return null;
          }
            回復  更多評論
            
          # re: struts中的文件上傳和下載
          2007-12-21 18:02 | yh
          謝謝上面這位了。  回復  更多評論
            
          主站蜘蛛池模板: 喜德县| 治县。| 芦溪县| 金堂县| 玉山县| 台中县| 顺昌县| 青川县| 洛浦县| 霍林郭勒市| 阿合奇县| 呈贡县| 平罗县| 宁晋县| 天祝| 板桥市| 紫阳县| 济源市| 兰西县| 衢州市| 金乡县| 嵊泗县| 丰宁| 德庆县| 辽宁省| 会泽县| 安西县| 桐城市| 武穴市| 鲜城| 汝阳县| 南郑县| 高阳县| 巴彦淖尔市| 湖州市| 漳浦县| 松溪县| 涡阳县| 邢台县| 思茅市| 盐边县|