jsp檔案上傳下載程式

來源:互聯網
上載者:User

package com.northking.dataManager.util;
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;jsp檔案上傳下載程式

/**
 * 使用sun.net.ftp工具包進行ftp上傳下載
 * @author maochangming
 * @date 2008-6-20  13:09:29
 * @description:
 */
public class FtpTool {
 String ip;
 int port;
 String user;
 String pwd;
 String remotePath;
 String localPath;
 FtpClient ftpClient;

 /**
  * 串連ftp伺服器
  * @param ip
  * @param port
  * @param user
  * @param pwd
  * @return
  * @throws Exception
  */
 public boolean connectServer(String ip, int port, String user, String pwd)
  throws Exception {
  boolean isSuccess = false;
  try {
   ftpClient = new FtpClient();
   ftpClient.openServer(ip, port);
   ftpClient.login(user, pwd);
   isSuccess = true;
  } catch (Exception ex) {
   throw new Exception("Connect ftp server error:" + ex.getMessage());
  }
  return isSuccess;
 }

 /**
  * 下載檔案
  * @param remotePath
  * @param localPath
  * @param filename
  * @throws Exception
  */
 public void downloadFile(String remotePath,String localPath, String filename) throws Exception {
  try {
   if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
    if (remotePath.length() != 0)
     ftpClient.cd(remotePath);
    ftpClient.binary();
    TelnetInputStream is = ftpClient.get(filename);
    File file_out = new File(localPath + File.separator + filename);
    FileOutputStream os = new FileOutputStream(file_out);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
     os.write(bytes, 0, c);
    }
    is.close();
    os.close();
    ftpClient.closeServer();
   }
  } catch (Exception ex) {
   throw new Exception("ftp download file error:" + ex.getMessage());
  }
 }

 /**
  * 上傳檔案
  * @param remotePath
  * @param localPath
  * @param filename
  * @throws Exception
  */
 public void uploadFile(String remotePath,String localPath, String filename) throws Exception {
  try {
   if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
    if (remotePath.length() != 0)
     ftpClient.cd(remotePath);
    ftpClient.binary();
    TelnetOutputStream os = ftpClient.put(filename);
    File file_in = new File(localPath + File.separator + filename);
    FileInputStream is = new FileInputStream(file_in);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
     os.write(bytes, 0, c);
    }
    is.close();
    os.close();
    ftpClient.closeServer();
   }
  } catch (Exception ex) {
   throw new Exception("ftp upload file error:" + ex.getMessage());
  }
 }

 /**
  * @return
  */
 public String getIp() {
  return ip;
 }

 /**
  * @return
  */
 public int getPort() {
  return port;
 }

 /**
  * @return
  */
 public String getPwd() {
  return pwd;
 }

 /**
  * @return
  */
 public String getUser() {
  return user;
 }

 /**
  * @param string
  */
 public void setIp(String string) {
  ip = string;
 }

 /**
  * @param i
  */
 public void setPort(int i) {
  port = i;
 }

 /**
  * @param string
  */
 public void setPwd(String string) {
  pwd = string;
 }

 /**
  * @param string
  */
 public void setUser(String string) {
  user = string;
 }

 /**
  * @return
  */
 public FtpClient getFtpClient() {
  return ftpClient;
 }

 /**
  * @param client
  */
 public void setFtpClient(FtpClient client) {
  ftpClient = client;
 }

 /**
  * @return
  */
 public String getRemotePath() {
  return remotePath;
 }

 /**
  * @param string
  */
 public void setRemotePath(String string) {
  remotePath = string;
 }

 /**
  * @return
  */
 public String getLocalPath() {
  return localPath;
 }

 /**
  * @param string
  */
 public void setLocalPath(String string) {
  localPath = string;
 }

}

下載測試

package com.northking.dataManager.dataimport.parse.test;

import com.northking.dataManager.util.FtpTool;

import junit.framework.TestCase;

/**
 * @author maochangming
 * @date 2008-6-20  13:09:11
 * @description:
 */
public class FtpToolTest extends TestCase {
 FtpTool ftpTool;

 /**
  * Constructor for FtpToolTest.
  * @param arg0
  */
 public FtpToolTest(String arg0) {
  super(arg0);
 }

 public static void main(String[] args) {
  junit.textui.TestRunner.run(FtpToolTest.class);
 }
 
 public void testDownLoadFile()throws Exception{
  ftpTool.downloadFile(ftpTool.getRemotePath(),"c:/downloads","JBFImgMng.CAB");
 }

 /*
  * @see TestCase#setUp()
  */
 protected void setUp() throws Exception {
  ftpTool = new FtpTool();
  ftpTool.setIp("10.164.12.70");
  ftpTool.setPort(2100);
  ftpTool.setUser("share");
  ftpTool.setPwd("share");
  ftpTool.setRemotePath("/paeams");
  super.setUp();
 }

 /*
  * @see TestCase#tearDown()
  */
 protected void tearDown() throws Exception {
  super.tearDown();
 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.