六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 57|回复: 0

远程共享文件读写操作

[复制链接]

升级  98%

11

主题

11

主题

11

主题

童生

Rank: 1

积分
49
 楼主| 发表于 2013-1-15 02:45:02 | 显示全部楼层 |阅读模式
package com.iman.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.List;import jcifs.smb.SmbException;import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;import jcifs.smb.SmbFileOutputStream;/**    *  @description:    远程共享读写文件操作工具类    *  @author:         luomeng*  @createDate:     2009-12-26 上午10:47:25 */public class RemoteFileUtil {public static final String REMOTE_HOST_IP = "192.168.*.*";public static final String LOGIN_ACCOUNT = "administrator";public static final String LOGIN_PASSWORD = "password";public static final String SHARE_DOC_NAME = "shareDoc";private String remoteHostIp;  //远程主机IPprivate String account;       //登陆账户private String password;      //登陆密码    private String shareDocName;  //共享文件夹名称                /**     * 默认构造函数     */    public RemoteFileUtil(){    this.remoteHostIp = REMOTE_HOST_IP;this.account = LOGIN_ACCOUNT;this.password = LOGIN_PASSWORD;this.shareDocName = SHARE_DOC_NAME;    }    /** * 构造函数 * @param remoteHostIp  远程主机Ip * @param account       登陆账户 * @param password      登陆密码 * @param sharePath     共享文件夹路径 */    public RemoteFileUtil(String remoteHostIp, String account, String password,String shareDocName) {this.remoteHostIp = remoteHostIp;this.account = account;this.password = password;this.shareDocName = shareDocName;}        /**     * 对远程共享文件进行读取所有行     * @param remoteFileName  文件名  说明:参数为共享目录下的相对路径     *                                若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);     *                                若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;     * @return  文件的所有行     */    public List<String> readFile(String remoteFileName){    SmbFile smbFile = null;    BufferedReader reader = null;    List<String> resultLines = null;    //构建连接字符串,并取得文件连接      String conStr = null;    conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;    try {smbFile = new SmbFile(conStr);} catch (MalformedURLException e) {e.printStackTrace();}        //创建readertry {reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(smbFile)));} catch (SmbException e) {e.printStackTrace();} catch (MalformedURLException e) {e.printStackTrace();} catch (UnknownHostException e) {e.printStackTrace();}//循环对文件进行读取        String line;try {line = reader.readLine();if(line != null && line.length()>0){resultLines = new ArrayList<String>();}while (line != null) {        resultLines.add(line);line = reader.readLine();        }} catch (IOException e) {e.printStackTrace();}        //返回return resultLines;    }        /**     * 对远程共享文件进行写入     * @param is                本地文件的输入流     * @param remoteFileName    远程文件名    说明:参数为共享目录下的相对路径     *                                             若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);     *                                             若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;     * @return       */    public boolean writeFile(InputStream is,String remoteFileName){    SmbFile smbFile = null;    OutputStream os = null;    byte[] buffer = new byte[1024*8];    //构建连接字符串,并取得文件连接      String conStr = null;    conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;    try {smbFile = new SmbFile(conStr);} catch (MalformedURLException e) {e.printStackTrace();return false;}//获取远程文件输出流并写文件到远程共享文件夹try {os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));while((is.read(buffer))!=-1){os.write(buffer);}} catch (Exception e) {e.printStackTrace();return false;}     return true;    }            /**     * 对远程共享文件进行写入重载     * @param localFileName   要写入的本地文件全名     * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径     *                                           若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);     *                                           若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;     * @return     */    public boolean writeFile(String localFileFullName ,String remoteFileName){    try {return writeFile(new FileInputStream(new File(localFileFullName)),remoteFileName);} catch (FileNotFoundException e) {e.printStackTrace();return false;}    }        /**     * 对远程共享文件进行写入重载     * @param localFileName   要写入的本地文件     * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径     *                                           若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);     *                                           若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;     * @return     */    public boolean writeFile(File localFile ,String remoteFileName){    try {return writeFile(new FileInputStream(localFile),remoteFileName);} catch (FileNotFoundException e) {e.printStackTrace();return false;}    }            public static void main(String[] args){    RemoteFileUtil util = new RemoteFileUtil();//    List<String> lines = util.readFile("test.txt");//    for (String string : lines) {//System.out.println(string);//}        util.writeFile(new File("f:/a.csv"), "new.csv");    }            }
   目前所在的项目,大量的用到了文件的上传。随着项目的扩大,用到了集群,这时候问题就出现了,用户上传到A上的文件下次用的时候服务可能在B上,这时文件就找不到了,所以我们的解决方案就是利用文件共享方式,把所有的文件放到了文件一个服务器上的共享文件夹中,作为文件服务器。所有的服务器写文件的时候都写到文件服务器中,这样就把问价给解决了。
    当然,现在这个工具类中还有加下载文件的方法,其实直接把远程文件读到文件流中,下载的时候用文件就可以了,具体方式可以参照readLines方法
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表