本地图片上传到远程的虚拟目录下,远程路径的图片上传到本地路径

        对于上传图片的要求,将本地图片上传到远程的虚拟目录下,该怎么实现?

        在远程计算机上,在IIS上发布一个虚拟目录,该步骤如下:

 

 的目录。

 

完成虚拟目录后,根据要求现要将本地的上传到虚拟目录下,用java代码实现如下:

1、首先需要下载一个jar包,即:jcifs-1.3.14.jar  (已经上传了)
 2、编写java代码:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import jcifs.util.LogStream;

/**
 *
 * @author 秦芬
 *@since 2013 -12-18
 *本地图片上传到远程的虚拟目录下
 */
public class Smb {

 /**
  * @param args
  */
 
  private static LogStream log = LogStream.getInstance();  //打印日志
  private String url = "";      
  private SmbFile smbFile = null;
  private SmbFileOutputStream smbOut = null;
  private static Smb smb = null; //共享文件协议
 
  public static synchronized Smb getInstance(String url) {
    if (smb == null)
     return new Smb(url);
    return smb;
   }
  /**
   * @param url服务器路径
   */
  private Smb(String url) {
   this.url = url;
   this.init();
  }

 public void init() {
   try {
    log.println("开始连接...url:" + this.url);
    smbFile = new SmbFile(this.url);
    smbFile.connect();
    log.println("连接成功...url:" + this.url);
   } catch (MalformedURLException e) {
    e.printStackTrace();
    log.print(e);
   } catch (IOException e) {
    e.printStackTrace();
    log.print(e);
   }
  }

 
  public int uploadFile(File file) {
    int flag = -1;
    BufferedInputStream bf = null;
    try {
     this.smbOut = new SmbFileOutputStream(this.url + "/" + file.getName(), 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);
     }
     flag = 0;
     log.println("文件传输结束...");
    } catch (SmbException e) {
     e.printStackTrace();
     log.println(e);
    } catch (MalformedURLException e) {
     e.printStackTrace();
     log.println(e);
    } catch (UnknownHostException e) {
     e.printStackTrace();
     log.println("找不到主机...url:" + this.url);
    } catch (IOException e) {
     e.printStackTrace();
     log.println(e);
    } finally {
     try {
      if (null != this.smbOut)
       this.smbOut.close();
      if (null != bf)
       bf.close();
     } catch (Exception e2) {
      e2.printStackTrace();
     }
    }

    return flag;
   }

 public static void main(String[] args) {
    String remoteUrl = "http://192.168.2.196/hopsca_images/"; 
    String localFile = "E:\\images\\a.jpg";  //本地要上传的文件
    File file = new File(localFile);
    Smb smb = Smb.getInstance(remoteUrl);
    smb.uploadFile(file);// 上传文件

 }

}
 该代码上传到Smb.rar压缩包中,可以直接拿来运行。

 

 

第二种就是将虚拟目录的文件保存到本地中,代码如下:

//虚拟目录的文件保存到本地中
 public static  boolean saveUrlAs(String longUrl, String localUrl) {
  //此方法只能用户HTTP协议
    try {
      URL url = new URL(longUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setDoOutput(true);
      /**从虚拟目录的文件保存到本地中**/
      DataInputStream in = new DataInputStream(connection.getInputStream());
      DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(localUrl)));
    
      byte[] buffer = new byte[4096];
      int count = 0;
      while ((count = in.read(buffer)) > 0) {
       out.write(buffer, 0, count);
      }
      out.close();
      in.close();
      System.out.println("成功====-");
      return true;
    }
    catch (Exception e) {
     e.printStackTrace();
    System.out.println("失败!!!!!!!!");
      return false;
    }
  }
 
 public static void test(){
   String longUrl = "http://192.168.2.196/hopsca_images/"; 
  String localUrl = "E:\\images\\a.jpg";
  saveUrlAs(longUrl, localUrl);
 }
 
 public static void main(String[] args) {
  test();
  System.out.println("测试");
  
 }

 

测试都通过了的

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值