tomcat集群 文件上傳


由於項目需求變動,原來的java web項目需要用到tomcat集群。運用集群之前需要先解決幾個小問題,現在就來說一下,關於文件上傳的問題(因為我之前文件是保存在服務器某個文件夾下,訪問服務器1tomcat上傳的文件,會有服務器2tomcat不能操作的情況):

lz菜鳥,簡單的把操作步驟介紹如下,如有不對的地方,歡迎大家留言討論,謝謝大家!

1.首先,把保存所有文件的文件夾共享;

2.用到的jar包,我用的是:jcifs-1.3.17.jar;

3.向共享目錄中上傳文件(共享文件名現命名為test)

      注:remoteUrl為文件服務器共享文件夾地址。

    不需要密碼:smb://192.168.0.100/test

    如果需要密碼:smb://username:password@192.168.0.100/test

    localFilePath本地文件地址,如:d:/test2

  //上傳文件到共享文件夾

  public static void smbPut(String remoteUrl,String localFilePath) {       
        InputStream in = null;       
        OutputStream out = null;       
        try {       
            File localFile = new File(localFilePath);      
            String fileName = localFile.getName();       
            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);       
            in = new BufferedInputStream(new FileInputStream(localFile));          
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));       
            byte[] buffer = new byte[1024];       
            while(in.read(buffer)!=-1){       
               out.write(buffer);       
               buffer = new byte[1024];       
            }       
        } catch (Exception e) {       
            e.printStackTrace();       
        } finally {       
            try {       
               out.close();       
               in.close();       
            } catch (IOException e) {       
               e.printStackTrace();       
            }       
        }       
     }

  上面代碼是在網上看到的,別人用了都是沒什么問題,但lz在使用過程中,當上傳word或excel文件的時候,從服務器直接復制過來會提示錯誤信息(提示部分內容  丟失,但文件可以打開,內容也可以看到)。搜了很多資料,原來是在字節流出錯。簡單的做個轉換就可解決,如下:

    private SmbFile smbFile = null;
      private SmbFileOutputStream smbOut = null;

  public static void smbPut(String remoteUrl,String localFilePath) {       
        //連接服務器
                 try {
                      smbFile = new SmbFile(remoteUrl);
                      smbFile.connect();
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  //上傳文件到服務器
                  BufferedInputStream bf = null;
                  File file= new File(localFilePath) ;
                  try {
                      this.smbOut = new SmbFileOutputStream(remoteUrl, false);
                      bf = new BufferedInputStream(new FileInputStream(file));
                      byte[] bt = new byte[8192];
                      int n = bf.read(bt);
                      while (n != -1) {
                        this.smbOut.write(bt, 0, n);
                        this.smbOut.flush();
                        n = bf.read(bt);
                      }
                  } catch (SmbException e) {
                      e.printStackTrace();
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                       try {
                           if (null != this.smbOut)
                           this.smbOut.close();
                           if (null != bf)
                             bf.close();
                       } catch (Exception e2) {
                           e2.printStackTrace();
                       }
                  }
     }

4.從共享目錄下載文件

  注:localDir為保存到本地路徑,如果不需要保存到本地,直接保存或另存為文件可以把操作localDir的代碼去掉。

  public static void smbGet(String remoteUrl,String localDir) {       
        InputStream in = null;       
        OutputStream out = null;       
        try {       
            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+filename);       
            if(remoteFile==null){       
               System.out.println("共享文件不存在");       
               return;       
            }       
            String fileName = remoteFile.getName();       
            File localFile = new File(localDir+File.separator+fileName);       
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

    //以下代碼為把文件保存到本地某個路徑下      
            out = new BufferedOutputStream(new FileOutputStream(localFile));          
            byte[] buffer = new byte[1024];       
            while(in.read(buffer)!=-1){       
               out.write(buffer);       
               buffer = new byte[1024];       
            }       
        } catch (Exception e) {       
            e.printStackTrace();       
        } finally {       
            try {       
               out.close();       
               in.close();       
            } catch (IOException e) {       
               e.printStackTrace();       
            }       
        }       
     }     

5.刪除共享目錄文件

  public void removeFile()throws Exception{
            int rs = 0;
            String filename = getRequest().getParameter("filename");
            if(!StringUtil.isBlank(filePath)){
                SmbFile file = new SmbFile(remoteUrl+"/"+filename);
                if(file.exists()){
                    //刪除附件 同時刪除數據庫表中的記錄
                    List<Attachment> actlist=getAttachments("htHouseProveFile4");
                    for(Attachment a:actlist){
                        String path=a.getAttachmentPath();
                        String delAttachmentHql = "delete ZTradDatilMsg trad where trad.filePath='"+path+"'";
                        PropertyChartService service =(PropertyChartService) ApplicationContextUtil.getBean("propertyChartService");
                        service.deleteHql(delAttachmentHql);
                    }
                    if(file.delete()){
                        rs =1;
                        }
                }
            }
            ajaxOutPrint(String.valueOf(rs));  //返回操作信息   1成功
        }

6.如果文件名有中文,需要轉碼

  //文件名稱轉碼   value為傳入文件名稱

  public static String filterDangerString(String value) {  
            if (value == null) {  
                return null;  
            }  
            value = value.replaceAll("\\|", "");  
      
            value = value.replaceAll("&", "&amp;");  
      
            value = value.replaceAll(";", "");  
      
            //value = value.replaceAll("@", "");  
      
            value = value.replaceAll("'", "");  
      
            value = value.replaceAll("\"", "");  
      
            value = value.replaceAll("\\'", "");  
      
            value = value.replaceAll("\\\"", "");  
      
            value = value.replaceAll("<", "&lt;");  
      
            value = value.replaceAll(">", "&gt;");  
      
            value = value.replaceAll("\\(", "");  
      
            value = value.replaceAll("\\)", "");  
      
            value = value.replaceAll("\\+", "");  
      
            value = value.replaceAll("\r", "");  
      
            value = value.replaceAll("\n", "");  
      
            value = value.replaceAll("script", "");  
              
            value = value.replaceAll("%27", "");  
            value = value.replaceAll("%22", "");  
            value = value.replaceAll("%3E", "");  
            value = value.replaceAll("%3C", "");  
            value = value.replaceAll("%3D", "");  
            value = value.replaceAll("%2F", "");  
            return value;  
        } 

  /**
     * 解碼
     * @param srcStr
     * @return
     */
    public static String urldecode(String srcStr){
        String str= null;
        try {
            str = java.net.URLDecoder.decode(srcStr,"utf-8");   
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
      return str ;
    }

后續還會補充。。。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM