乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      CSDN技術(shù)中心 有關(guān)FileUpload組件的使用和調(diào)試的經(jīng)驗(yàn)

       larrin 2006-03-22

      在通過使用FileUpload組件上傳的過程中,通過自己的調(diào)試,總結(jié)如下:
      1)使用之前的準(zhǔn)備,我用的是commons-fileupload-1.1-dev.jar和commons-io-1.1-dev.jar。
         解釋一下:盡管有的資料解釋是commons-fileupload-1.0-beta.jar和commons-beanutils.jar,通過調(diào)試的結(jié)果
         顯示并不是需要commons-beanutils.jar文件,而是由于在parseRequest(request)的類有關(guān)繼承于DiskFileItem
      類。而他有private  org.apache.commons.io.output.DeferredFileOutputStream dfos。這樣的就必須使用到commons-io-1.1-dev.jar。因此需要導(dǎo)入該包。否則就出classNotFound:.DeferredFileOutputStream的錯(cuò)誤。

      2)由于涉及文件,就涉及到文件系統(tǒng)。然而在java或應(yīng)用服務(wù)器中對(duì)于文件系統(tǒng)的訪問,就有一定的安全策略。
        需要將下列權(quán)限添加到您應(yīng)用程序服務(wù)器的安全策略文件中:
      permission java.io.FilePermission "<<ALL FILES>>", "read,write,delete";
      具體是添加到..\bea\weblogic81\server\lib\weblogic.policy中的.
      否則會(huì)可能出如下異常錯(cuò)誤:
       org.apache.commons.fileupload.FileUploadException:
      java.lang.reflect.InvocationTargetException
      at
      org.apache.commons.fileupload.FileUpload.createItem(FileUpload.java:615)
      at
      org.apache.commons.fileupload.FileUpload.parseRequest(FileUpload.java:474)
      at
      org.apache.commons.fileupload.FileUpload.parseRequest(FileUpload.java:355)
      ....

      3)對(duì)于不同的服務(wù)器,在調(diào)試的過程中會(huì)出各種不一樣的結(jié)果。這個(gè)與具體的服務(wù)器有關(guān)。

      4)由于FileUpload在不斷的更新版本,它的很多方法已經(jīng)不推薦使用了(這與該組件的不斷的改進(jìn)有關(guān))。通過對(duì)最新的幫助文檔和網(wǎng)上的資料寫了一個(gè)標(biāo)準(zhǔn)的程序如下:
      fileUpload文件:
      import java.io.IOException;
      import java.io.PrintWriter;

      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import org.apache.commons.fileupload.*;
      import java.util.*;
      import java.util.regex.*;
      import java.io.*;
      import org.apache.commons.fileupload.servlet.*;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;

      /*
       * 創(chuàng)建日期 2005-4-10
       *
       * TODO 要更改此生成的文件的模板,請(qǐng)轉(zhuǎn)至
       * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
       */

      /**
       * @author gaolong1
       *
       * TODO 要更改此生成的類型注釋的模板,請(qǐng)轉(zhuǎn)至
       * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
       */
      public class FileUpload extends HttpServlet {

       /**
        * Destruction of the servlet. <br>
        */
       private String uploadPath = "D:\\addnetFile\\"; // 用于存放上傳文件的目錄
          private File tempPath =new File("D:\\addnetFile\\tmp\\"); // 用于存放臨時(shí)文件的目錄
       public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
       }

       /**
        * The doPost method of the servlet. <br>
        *
        * This method is called when a form has its tag value method equals to post.
        *
        * @param request the request send by the client to the server
        * @param response the response send by the server to the client
        * @throws ServletException if an error occurred
        * @throws IOException if an error occurred
        */
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
         res.setContentType( "text/html; charset=GB2312");
        PrintWriter out=res.getWriter();
        System.out.println(req.getContentLength());
           System.out.println(req.getContentType());
         DiskFileItemFactory factory = new DiskFileItemFactory();
              // maximum size that will be stored in memory
              factory.setSizeThreshold(4096);
              // the location for saving data that is larger than getSizeThreshold()
              factory.setRepository(new File("d:\\File\\addnetFile\\temp"));

              ServletFileUpload upload = new ServletFileUpload(factory);
              // maximum size before a FileUploadException will be thrown
              upload.setSizeMax(1000000);
              try{
              List fileItems = upload.parseRequest(req);
              // assume we know there are two files. The first file is a small
              // text file, the second is unknown and is written to a file on
              // the server
              Iterator iter = fileItems.iterator();

      //  正則匹配,過濾路徑取文件名
           String regExp=".+\\\\(.+)$";

      //  過濾掉的文件類型
        String[] errorType={".exe",".com",".cgi",".asp"};
           Pattern p = Pattern.compile(regExp);
              while (iter.hasNext()) {
               FileItem item = (FileItem)iter.next();
               //忽略其他不是文件域的所有表單信息
               if (!item.isFormField()) {
                   String name = item.getName();
                   long size = item.getSize();
                   if((name==null||name.equals("")) && size==0)
                       continue;
                Matcher m = p.matcher(name);
               boolean result = m.find();
               if (result){
                   for (int temp=0;temp<errorType.length;temp++){
                   if (m.group(1).endsWith(errorType[temp])){
                         throw new IOException(name+": wrong type");
                   }
                   }
                   try{

      //        保存上傳的文件到指定的目錄

      //        在下文中上傳文件至數(shù)據(jù)庫時(shí),將對(duì)這里改寫
                           item.write(new File("d:\\" + m.group(1)));

                         out.print(name+"  "+size+"<br>");
                         }
                         catch(Exception e){
                           out.println(e);
                         }

                      }
                     else
                     {
                       throw new IOException("fail to upload");
                     }
                     }
                 }
              }
               catch (IOException e){
                 out.println(e);
               }
               catch (FileUploadException e){
                    out.println(e);
               }

      //  保存上傳的文件到指定的目錄

      //  在下文中上傳文件至數(shù)據(jù)庫時(shí),將對(duì)這里改寫

          }


       /**
        * Initialization of the servlet. <br>
        *
        * @throws ServletException if an error occure
        */
       public void init() throws ServletException {
        // Put your code here
       }

      }
      對(duì)應(yīng)的請(qǐng)求文件:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
          <title>index.html</title>

          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          <meta http-equiv="description" content="this is my page">

          <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

        </head>

        <body>
         <form action="./servlet/FileUpload" method="post" enctype="multipart/form-data" name="form1">
        <input type="file" name="file">
        <input type="submit" name="Submit" value="upload">
      </form>
         <form action="./servlet/HelloWord" method="post">
          <input type="submit"/>
          </form>
          <form name="uploadform" method="POST" action="./servlet/FileUpload" ENCTYPE="multipart/form-data">

              <table border="1" width="450" cellpadding="4" cellspacing="2" bordercolor="#9BD7FF">

              <tr><td width="100%" colspan="2">

                              文件1:<input name="x" size="40" type="file">

              </td></tr>

              <tr><td width="100%" colspan="2">

                              文件2:<input name="y" size="40" type="file">

              </td></tr>

              <tr><td width="100%" colspan="2">

                              文件3:<input name="z" size="40" type="file">

              </td></tr>

              </table>

              <br/><br/>

              <table>

              <tr><td align="center"><input name="upload" type="submit" value="開始上傳"/></td></tr>

             </table>

      </form>

        </body>
      </html>
      注:該代碼由部分來自網(wǎng)上!

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多