tomcat集群 文件上传

mac2022-06-30  18

由于项目需求变动,原来的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("&", "&");                    value = value.replaceAll(";", "");                    //value = value.replaceAll("@", "");                    value = value.replaceAll("'", "");                    value = value.replaceAll("\"", "");                    value = value.replaceAll("\\'", "");                    value = value.replaceAll("\\\"", "");                    value = value.replaceAll("<", "<");                    value = value.replaceAll(">", ">");                    value = value.replaceAll("\\(", "");                    value = value.replaceAll("\\)", "");                    value = value.replaceAll("\\+", "");                    value = value.replaceAll("\r", "");                    value = value.replaceAll("\n", "");                    value = value.replaceAll("script", "");                            value = value.replaceAll("'", "");              value = value.replaceAll(""", "");              value = value.replaceAll(">", "");              value = value.replaceAll("<", "");              value = value.replaceAll("=", "");              value = value.replaceAll("/", "");              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 ;    }

后续还会补充。。。。。

 

转载于:https://www.cnblogs.com/Allen0603/p/4026544.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)