上传文件

来源:互联网 发布:遇见淘宝店 编辑:程序博客网 时间:2024/06/05 08:24

以供自己备忘。

 

 

jsp 页面

注意下面的黑体部分

  1. <body>
  2.     <html:form action="upload" enctype="multipart/form-data">
  3.         <html:file property="file"></html:file>
  4.         <html:submit></html:submit>
  5.     </html:form>
  6. </body>

下面是formBean 主要是封装一个file

  1. public class UpLoadForm extends ActionForm {
  2.     /**
  3.      * 
  4.      */
  5.     private static final long serialVersionUID = -5778426661470052057L;
  6.     private FormFile file ;
  7.     
  8.     public FormFile getFile() {
  9.         return file;
  10.     }
  11.     public void setFile(FormFile file) {
  12.         this.file = file;
  13.     }
  14. }

 

下面action 里面的代码

  1. package upload.action;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.UnsupportedEncodingException;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.struts.action.Action;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionForward;
  14. import org.apache.struts.action.ActionMapping;
  15. import org.apache.struts.upload.FormFile;
  16. import upload.form.UpLoadForm;
  17. public class UpLoadAction extends Action {
  18.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  19.             HttpServletRequest request, HttpServletResponse response) {
  20.         
  21.         if (form instanceof UpLoadForm) {
  22.             String encoding = request.getCharacterEncoding();
  23.             if(encoding!=null&&encoding.equalsIgnoreCase("utf-8"))
  24.             {
  25.                 response.setContentType("text/html;charset=gb2312");
  26.             }
  27.             UpLoadForm tf = (UpLoadForm) form ;
  28.             FormFile file = tf.getFile();               //  get the upload file
  29.             String contentType = file.getContentType();
  30.             
  31.             String size = file.getFileSize()+"byte";    //  the upload size
  32.             String fn = file.getFileName();             //  get the upload file name
  33.             
  34.             try {
  35.                 InputStream stream = file.getInputStream(); //  read the file
  36.                 String path = this.getServlet().getServletContext().getRealPath("/");       //  get the save path
  37. //              OutputStream ops = new FileOutputStream(this.checkFileExist(path+"upload")+"/"+fn); //  放在自己建的目录下,新建的目录是唯一的
  38.                 OutputStream ops = new FileOutputStream(path+"upload"+"/"+fn);  //  放在这个目录下
  39.                 
  40.                 int br = 0;
  41.                 byte[] bf = new byte[8192];
  42.                 while ((br=stream.read(bf, 0, bf.length))!=-1) {
  43.                     ops.write(bf,0,br);
  44.                 }
  45.                 
  46.                 ops.close();
  47.                 stream.close();
  48.                 
  49.                 request.setAttribute("contentType", contentType);
  50.                 request.setAttribute("size", size);
  51.                 request.setAttribute("fn", fn);
  52.                 
  53.                 return mapping.findForward("display");
  54.             } catch (FileNotFoundException e) {
  55.                 e.printStackTrace();
  56.                 System.out.println("not found the upload file");
  57.             } catch (IOException e) {
  58.                 e.printStackTrace();
  59.                 System.out.println("io exception ");
  60.             }
  61.             
  62.         }
  63.         return null;
  64.     }
  65.     /**
  66.      * 创建自己存放上传文件的文件夹
  67.      * @param str
  68.      * @return filePath 文件夹的路径
  69.      */
  70.     private String getOwnFilePath(String str)
  71.     {
  72.         String s = "";
  73.         try {
  74.             
  75.             File ownFilePath = new File(str);
  76.             File ofp2 =  new File(str+"(1)");
  77.             if(ownFilePath.exists())
  78.             {
  79.                 ownFilePath.renameTo(ofp2);
  80.                 ownFilePath.mkdir();
  81.                 s = ownFilePath.getPath();
  82.             }
  83.             else
  84.             {
  85.                 ownFilePath.mkdir();
  86.                 s = ownFilePath.getPath();
  87.             }
  88.         } catch (Exception e)
  89.         {
  90.             e.printStackTrace();
  91.             System.out.println("errory cause by create filePath");
  92.         }
  93.         return s;
  94.     }
  95.     /**
  96.      * 判断要创建的文件夹是否存在<br>
  97.      * 存在 就在此文件夹名后面加(1)<br>
  98.      * 直到没有重复名称为止,返回可用的那个
  99.      * @param str 要创建的路径和文件名
  100.      * @return file
  101.      */
  102.     private static File checkFileExists(String str)
  103.     {
  104.         File fp = new File(str) ;
  105.         boolean flag = fp.exists();
  106.         String path = str;  //  根据需要可以将此路径返回去
  107.         for(;flag;)
  108.         {
  109.             path = fp.toString()+"(1)";
  110.             fp = new File(path);
  111.             flag = fp.exists();
  112.         }
  113.         return fp ;
  114.     }
  115.     /**
  116.      * 判断要创建的文件夹是否存在<br>
  117.      * 存在 就在此文件夹名后面加(1)<br>
  118.      * 直到没有重复名称为止,返回可用的那个
  119.      * @param str 要创建的路径和文件名
  120.      * @return file
  121.      */
  122.     private static String checkFileExist(String str)
  123.     {
  124.         File fp = new File(str) ;
  125.         boolean flag = fp.exists();
  126.         String path = str;  //  根据需要可以将此路径返回去
  127.         for(;flag;)
  128.         {
  129.             path = fp.toString()+"(1)";
  130.             fp = new File(path);
  131.             flag = fp.exists();
  132.         }
  133.         try {
  134.             fp.mkdir();
  135.         } catch (Exception e)
  136.         {
  137.             e.printStackTrace();
  138.         }
  139.         return path ;
  140.     }
  141. }

 

下面是struts-cfg.xml中的部分配置

  1.  <form-bean name="uploadForm" type="upload.form.UpLoadForm" />
  2.   <global-exceptions />
  3.   <global-forwards />
  4.   <action-mappings >    
  5.     <!-- upload file  -->
  6.     <action 
  7.         path="/upload" 
  8.         name="uploadForm"
  9.         type="upload.action.UpLoadAction">
  10.          <forward name="display" path="/upload/display.jsp" />
  11.     </action>
原创粉丝点击