posts - 167,  comments - 30,  trackbacks - 0
          最近通過做的項目中修改個bug,在jsp頁面中潛入applet,通過applet來上傳5-10G之間的文件。
          修改過程記錄如下:
          JSP頁面中
                          <div id="appletTemp">   
                                 <applet code = "org/apache/commons/yp/MyApplet.class"  name="test" archive="applet.jar"
                                       height="32" width="260" mayscript>
                                  <s:text name="image.add.installJdk"></s:text>
                                   </applet>
                            </div>   

          以下為Java Applet結合commons-net-2.0.jar包實現上傳文件的功能代碼:
          package org.apache.commons.yp;

          import java.applet.Applet;
          import java.awt.BorderLayout;
          import java.awt.Font;
          import java.awt.HeadlessException;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.io.File;
          import java.io.IOException;
          import javax.swing.JButton;
          import javax.swing.JFileChooser;
          import javax.swing.JTextField;
          import javax.swing.filechooser.FileFilter;
          import netscape.javascript.JSObject;

          public class MyApplet extends Applet implements ActionListener {
              
          public void destroy() {
              }

              
          public String getAppletInfo() {
                  
          return "This is my default applet created by Eclipse";
              }

              
          public void init() {
              }

              
          public void start() {
              }

              
          public void stop() {
              }

              
          private static final long serialVersionUID = 1L;

              
          private MyApplet jFrame = null;

              
          private JButton jFileButton = null;
              
          private JButton onClickButton = null;
              JTextField jtf 
          = new JTextField(10);
              File fileIn 
          = null;
              FileFtpApplet myFtp 
          = new FileFtpApplet();
              
          public int totalSize = 0;
              
          public int nowSize = 0;
              
          public String hostName;
              
          public Integer port;
              
          public String user;
              
          public String password; 
              
              
          public String getHostName() {
                  
          return hostName;
              }

              
          public void setHostName(String hostName) {
                  
          this.hostName = hostName;
              }

              
          public Integer getPort() {
                  
          return port;
              }

              
          public void setPort(Integer port) {
                  
          this.port = port;
              }

              
          public String getUser() {
                  
          return user;
              }

              
          public void setUser(String user) {
                  
          this.user = user;
              }

              
          public String getPassword() {
                  
          return password;
              }

              
          public void setPassword(String password) {
                  
          this.password = password;
              }

              
          public MyApplet() {
                  System.out.println(
          " MyApplet start ");
                  jFrame 
          = this;
                  jFrame.setSize(
          5010);
                  jFileButton 
          = new JButton("瀏覽");
                  jFileButton.setFont(
          new Font("system", Font.PLAIN, 12));
                  jFileButton.setSize(
          22);
                  onClickButton 
          = new JButton("上傳");
                  onClickButton.setFont(
          new Font("system", Font.PLAIN, 12));
                  jFrame.add(jtf, BorderLayout.NORTH);
                  jFrame.add(jFileButton);
                  jFrame.add(onClickButton);
                  jtf.setEditable(
          true);
                  onClickButton.addActionListener((ActionListener) 
          this);
                  jFileButton.addActionListener((ActionListener) 
          this);
                  onClickButton.setSize(
          220);
                  System.out.println(
          " MyApplet end ");
                  
          this.start();
              }

              
          public void actionPerformed(ActionEvent e) {
                  System.out.println(
          " actionPerformed(ActionEvent e)  ");
                  String comm 
          = e.getActionCommand();
                  
          try {
                      
          if ("瀏覽".equals(comm)) {
                          JFileChooser jfChooser 
          = new JFileChooser("");
                          jfChooser.setDialogTitle(
          "打開并上傳文件");
                          jfChooser.setFileFilter(
          new FileFilter() {
                              @Override
                              
          public boolean accept(File f) {
                                  
          if (f.getName().endsWith("img"|| f.isDirectory())
                                      
          return true;
                                  
          return false;
                              }

                              @Override
                              
          public String getDescription() {
                                  
          return "鏡像文件(*.img)";
                              }
                          });
                          
          int result = jfChooser.showOpenDialog(jFrame);
                          
          if (result == JFileChooser.APPROVE_OPTION) {
                              fileIn 
          = jfChooser.getSelectedFile();
                              jtf.setText(fileIn.getPath());
                              
          try {
                                  JSObject.getWindow(
          this).eval(
                                          
          "getinfo(" + "\"" + fileIn.getName() + "\""
                                                  
          + ")");
                              } 
          catch (Exception ea) {
                                  ea.printStackTrace();
                              }
                              
          if (fileIn != null && fileIn.exists()) {
                                  
          try {
                                      
          long l1 = System.currentTimeMillis();
                                      System.out.println(hostName
          +"----hostName");
                                      System.out.println(port
          +"----port");
                                      System.out.println(user
          +"-----user");
                                      System.out.println(password
          +"---------password");
                                      
          if (myFtp.connect(hostName, port, user, password)) {
                                          myFtp.setLocal(fileIn.getPath());
                                          myFtp.setFileName(fileIn.getName());
                                          myFtp.setRemote(
          "../../opt/nebula/images");
                                          
          long l2 = System.currentTimeMillis();
                                          System.out.println(
          "end:" + l2);
                                          System.out.println(
          "remaining:" + (l2 - l1));
                                      }
                                  } 
          catch (Exception e1) {
                                      System.out.println(
          "連接出錯 " + e1.getMessage());
                                  }
                              }
                          }
                      } 
          else if ("上傳".equals(comm)) {
                          
          try {
                              JSObject.getWindow(
          this).eval(
                                      
          "javascript:showLoading(" + "\"" + fileIn.getName()
                                              + "\"" + ")");
                          } catch (Exception ea) {
                              ea.printStackTrace();
                          }
                          myFtp.upload(hostName, port, user, password);
                          
          try {
                              JSObject.getWindow(
          this).eval(
                                      
          "javascript:uploadEnd(" + "\"" + fileIn.getName()
                                              + "\"" + ")");
                          } catch (Exception ea) {
                              ea.printStackTrace();
                          }
                      }
                  } 
          catch (HeadlessException e1) {
                      e1.printStackTrace();
                  } 
          finally {
                      
          try {
                          myFtp.disconnect();
                      } 
          catch (IOException e1) {
                          e1.printStackTrace();
                      }
                  }
              }
          }

          package org.apache.commons.yp;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.OutputStream;
          import java.io.RandomAccessFile;
          import org.apache.commons.net.ftp.FTPClient;
          import org.apache.commons.net.ftp.FTPFile;
          import org.apache.commons.net.ftp.FTPReply;

          /**
           * 
           * Function Description:FTP 鍔熻兘綾伙紝鍚玴ut鍜実et鏂規硶
           
          */

          public class FileFtpApplet {
              
          public static FTPClient ftp = new FTPClient();
              
          public String remote;
              
          public String fileName;
              
          public String local;

              
          public String getRemote() {
                  
          return remote;
              }

              
          public void setRemote(String remote) {
                  
          this.remote = remote;
              }

              
          public String getFileName() {
                  
          return fileName;
              }

              
          public void setFileName(String fileName) {
                  
          this.fileName = fileName;
              }

              
          public String getLocal() {
                  
          return local;
              }

              
          public void setLocal(String local) {
                  
          this.local = local;
              }

              
          public boolean upload(String hostname, Integer port, String username,
                      String password) {
                  
          try {
                      
          this.connect(hostname, port, username, password);
                  } 
          catch (IOException e1) {
                      e1.printStackTrace();
                  }
                  ftp.enterLocalPassiveMode();
                  
          try {
                      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          try {
                      ftp.changeWorkingDirectory(
          "");
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          boolean result;
                  
          try {
                      createDir(remote);
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          boolean isExists = false;
                  
          long fileSize = 0;
                  FTPFile[] files 
          = new FTPFile[10];
                  
          try {
                      files 
          = ftp.listFiles();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          for (int i = 0; i < files.length; i++) {
                      FTPFile file 
          = files[i];
                      
          if (fileName.equals(file.getName())) {
                          isExists 
          = true;
                          fileSize 
          = file.getSize();
                          
          break;
                      }
                  }
                  
          if (isExists) {
                      System.out.println(
          "fileSize:" + fileSize);
                      File fLocal 
          = new File(local);
                      OutputStream os;
                      
          try {
                          os 
          = ftp.appendFileStream(fileName);
                          RandomAccessFile randomAccessFileLocal;
                          randomAccessFileLocal 
          = new RandomAccessFile(fLocal, "r");
                          randomAccessFileLocal.seek(fileSize);
                          
          int len = 0;
                          
          byte[] bt = new byte[1024];
                          
          while ((len = randomAccessFileLocal.read(bt)) > 0) {
                              os.write(bt, 
          0, len);
                          }
                          randomAccessFileLocal.close();
                          os.close();
                          result 
          = true;
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }

                  } 
          else {
                      
          try {
                          File f 
          = new File(local);
                          FileInputStream in 
          = new FileInputStream(f);
                          result 
          = ftp.storeFile(fileName, in);
                          in.close();
                          
          return result;
                      } 
          catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
                  
          return false;
              }

              
          /**
               * 寤虹珛FTP榪炴帴
               
          */
              
          public boolean connect(String hostname, int port, String username,
                      String password) 
          throws IOException {
                  ftp.connect(hostname, port);
                  ftp.login(username, password);
                  
          if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                      disconnect();
                      
          return false;
                  }
                  
          return true;
              }

              
          /**
               * 鍏抽棴FTP榪炴帴
               
          */
              
          public void disconnect() throws IOException {
                  
          if (ftp.isConnected()) {
                      ftp.disconnect();
                  }
              }

              
          public void createDir(String dir) throws IOException {
                  
          if (dir != null && dir.trim().length() > 0) {
                      String[] dirs 
          = dir.split("/");
                      String workingDir 
          = "";
                      
          for (int i = 0; i < dirs.length; i++) {
                          
          if (!existDirectory(workingDir, dirs[i])) {
                              ftp.makeDirectory(dirs[i]);
                              workingDir 
          = dirs[i];
                              ftp.changeWorkingDirectory(dirs[i]);
                          } 
          else {
                              workingDir 
          = dirs[i];
                              ftp.changeWorkingDirectory(dirs[i]);
                          }
                      }
                  }
              }

              
          public boolean existDirectory(String path, String dirName)
                      
          throws IOException {
                  
          boolean flag = false;
                  FTPFile[] ftpFileArr 
          = ftp.listFiles(path);
                  
          for (FTPFile ftpFile : ftpFileArr) {
                      
          if (ftpFile.isDirectory()
                              
          && ftpFile.getName().equalsIgnoreCase(dirName)) {
                          flag 
          = true;
                          
          break;
                      }
                  }
                  
          return flag;
              }
          }
          訪問該jsp頁面,第一次會提示“請安裝jdk” ,那么就按照他所說的,去
          http://www.java.com/下載jre進行安裝。(但是我自己服務器上已經有jdk,為什么默認找到里面的jre呢,在研究中。。。)
          控制臺中打印的信息:
          Java Plug-in 1.6.0_26
          使用 JRE 版本 1.6.0_26-b03 Java HotSpot(TM) Client VM
          用戶主目錄 = C:\Documents and Settings\server

          ----------------------------------------------------
          c:   清除控制臺窗口
          f:   終結在結束隊列上的對象
          g:   垃圾收集
          h:   顯示此幫助消息
          l:   轉儲類載入程序列表
          m:   打印內存使用
          o:   觸發日志記錄
          q:   隱藏控制臺
          r:   重新載入策略配置
          s:   轉儲系統和部署屬性
          t:   轉儲線程列表
          v:   轉儲線程堆棧
          x:   清除類載入程序高速緩存
          0-5: 設置跟蹤級別為<n>
          ----------------------------------------------------
          。。。。。。。。。。。。。。。。。。。。。。。。。。
          安裝完成jre后,報錯 ClassNotFoundException找不到應用中的org/apache/commons/yp/MyApplet.class文件.
          因為archive="applet.jar",默認找的是工程WebRoot下面的See the line -:
          applet.jar,所以需要將MyApplet.java打包為applet.jar中并放到WebRoot下。
          訪問還是報錯說找不到org/apache/commons/net/ftp/FTPClient.class類,我工程中的lib下已經存在commons-net-2.0.jar,找的應該是默認在applet.jar中類,
          所以我又將
          commons-net-2.0.jar
          中所有的class文件放到了applet.jar中。在訪問不存在找不到類的問題了。

          但是又出現了新的問題,說
          Caused by: java.security.AccessControlException: access denied (java.io.FilePermission <<ALL FILES>> read)等相關的異常。
          解決方案如下:
          Edit and append the snippet below, to the end of the policy file (The typical location of your policy file is C:\Program Files\Java\jre1.5.0_16\lib\security\java.policy). If you are running a different JRE (i.e. 1.6), then the folder name 'jre1.5.0_16' will obviously reflect your version of the java runtime environment.

          在你自己的jre\lib\security\java.policy文件中增加如下配置。對于文件的訪問許可。
          grant codeBase "http://*/-" {
              permission java.security.AllPermission;
              permission java.io.FilePermission "<<ALL FILES>>", "read";
              permission java.io.FilePermission "<<ALL FILES>>", "write";
          };
          經過測試,以上配置可用。
          java applet界面如下:

          以上問題解決之后,點擊瀏覽,彈出框選擇文件,之后點擊上傳。
          查看jre控制臺說連接不上ftp 21端口,Connected refused Exception,
          那就說明linux的ftp 21端口沒有開啟。看如下解決方案:

          1-ssh以root用戶身份登錄 登錄Linux系統后,

          vi /etc/xinetd.d/gssftp

          # default: off
          # description: The kerberized FTP server accepts FTP connections \
          #              that can be authenticated with Kerberos 5.
          service ftp
          {
                  flags           = REUSE
                  socket_type     = stream
                  wait            = no
                  user            = root
                  server          = /usr/kerberos/sbin/ftpd
                  server_args     = -l  -a
                  log_on_failure  += USERID
                  disable         = yes
          }
          server_args     = -l  -a 修改為 server_args     = -l
          disable         = yes 修改 disable         = no
          然后執行
          [root@vm-container-16-5 sbin]# service xinetd restart
          Stopping xinetd:                                           [  OK  ]
          Starting xinetd:                                           [  OK  ]
          OK!!!!

          2- 執行service xinetd restart如果出現
          xinetd: unrecognized service
          則可能說明安裝操作系統時默認未安裝xinetd
          通過yum -y install xinetd
          [root@vm-container-16-5 sbin]# service xinetd restart
          xinetd: unrecognized service
          [root@vm-container-16-5 sbin]# yum -y install xinetd
          Rocks-5.3                                                                                                                                                                          | 1.1 kB     00:00     
          Setting up Install Process
          Resolving Dependencies
          There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
          The program yum-complete-transaction is found in the yum-utils package.
          --> Running transaction check
          ---> Package xinetd.x86_64 2:2.3.14-10.el5 set to be updated
          --> Finished Dependency Resolution

          Dependencies Resolved

          ==========================================================================================================================================================================================================
           Package                                      Arch                                         Version                                                  Repository                                       Size
          ==========================================================================================================================================================================================================
          Installing:
           xinetd                                       x86_64                                       2:2.3.14-10.el5                                          Rocks-5.3                                       125 k

          Transaction Summary
          ==========================================================================================================================================================================================================
          Install      1 Package(s)         
          Update       0 Package(s)         
          Remove       0 Package(s)         

          Total download size: 125 k
          Downloading Packages:
          xinetd-2.3.14-10.el5.x86_64.rpm                                                                                                                                                    | 125 kB     00:00     
          Running rpm_check_debug
          Running Transaction Test
          Finished Transaction Test
          Transaction Test Succeeded
          Running Transaction
            Installing     : xinetd                                                                                                                                                                             1/1

          Installed:
            xinetd.x86_64 2:2.3.14-10.el5                                                                                                                                                                           

          Complete!
          [root@vm-container-16-5 sbin]# service xinetd restart      
          Stopping xinetd:                                           [FAILED]
          Starting xinetd:                                           [  OK  ]
          [root@vm-container-16-5 sbin]# service xinetd restart
          Stopping xinetd:                                           [  OK  ]
          Starting xinetd:                                           [  OK  ]

          Linux下如果FTP訪問不了可以進行如下設置修改:

          在終端中執行 vi /etc/selinux/config,將其中的selinux=enforcing的enforcing改為disable。然后保存退出vi,執行setenforce 0 或者重啟。就可以上傳了。

          如果,你不是上述的解決方法的話,有可能是你沒有到ftp服務器下的incoming文件夾中,或者你沒有把incoming文件夾的權限改為777。

          ELinux是「Security-Enhanced Linux」的簡稱,是美國國家安全局「NSA=The National Security Agency」 和SCC(Secure Computing Corporation)開發的 Linux的一個擴張強制訪問控制安全模塊。原先是在Fluke上開發的,2000年以 GNU GPL 發布。

          posted on 2011-06-21 15:07 David1228 閱讀(2259) 評論(0)  編輯  收藏 所屬分類: Linux

          <2011年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章檔案

          新聞分類

          新聞檔案

          相冊

          收藏夾

          Java

          Linux知識相關

          Spring相關

          云計算/Linux/虛擬化技術/

          友情博客

          多線程并發編程

          開源技術

          持久層技術相關

          搜索

          •  

          積分與排名

          • 積分 - 359341
          • 排名 - 154

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 曲松县| 遵化市| 龙泉市| 普陀区| 安徽省| 祁阳县| 克什克腾旗| 万全县| 错那县| 密云县| 宝清县| 南平市| 深圳市| 遂宁市| 鲁甸县| 昌宁县| 连州市| 沧源| 顺义区| 墨竹工卡县| 黄骅市| 谢通门县| 乐昌市| 诸城市| 徐水县| 金坛市| 定边县| 安仁县| 牡丹江市| 贡嘎县| 方城县| 微博| 简阳市| 晋中市| 榕江县| 怀柔区| 绥化市| 东阳市| 明溪县| 当阳市| 开阳县|