轉:http://www.128kj.com/article/article5/4C981624F129B81E393E4DFE72AC9096.htm?id=1820


前幾天寫過一編"關于java的http協議文件上傳實用例題"的文章;今天還想寫編關于java用ftp上傳文件的內容。我來說說2者的優缺點;
    1:用http協議上傳更適合web編程的方便;傳小于1M文件速度要比用ftp協議上傳文件略快。安全性好;不像ftp那樣;必須要啟動一個ftp服務才行。

    2:用ftp協議上傳文件大于1M的文件速度比http快;文件越大;上傳的速度就比http上傳快的倍數越大。而且用java編寫程序;ftp比http方便。好,廢話少說;我們先搭建一個實例來理性認識一下用java編寫ftp上傳文件的技術。

  首先在本機啟動一個ftp服務,ftp的用戶:"IUSR_ZJH" 密碼:"123";隨后在ftp主目錄下建一個名為upftp的子目錄;下面有4個文件就可啟動這個例題了。

文件1:MainCtrl.java(servlet文件)內容如下:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import java.io.FileInputStream;
import java.io.IOException;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;


public class MainCtrl extends HttpServlet {
 
   private FtpClient ftpClient;
  
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException {

   resp.setContentType("text/html; charset=UTF-8");

   try {
    //連接ftp服務器
    connectServer("127.0.0.1", "IUSR_ZJH", "123", "upftp");
  //上傳文件;并返回上傳文件的信息
    req.setAttribute("inf", upload(req.getParameter("file_name")));
   } catch (Exception e) {
  System.out.println(e.toString());
  req.setAttribute("inf", e.toString());
  req.getRequestDispatcher("view_inf.jsp").forward(req, resp);
  return;
   } finally {
    if (ftpClient != null) {
     ftpClient.closeServer();    
    }
      }
   req.getRequestDispatcher("view_inf.jsp").forward(req, resp);
  }
 
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   doPost(req, resp);
  }
//連接ftp服務器
 private void connectServer(String server, String user, String password,
   String path) throws IOException {
  // server:FTP服務器的IP地址;user:登錄FTP服務器的用戶名
  // password:登錄FTP服務器的用戶名的口令;path:FTP服務器上的路徑
  ftpClient = new FtpClient();
  ftpClient.openServer(server);
  ftpClient.login(user, password);
  //path是ftp服務下主目錄的子目錄
  if (path.length() != 0)
   ftpClient.cd(path);
  //用2進制上傳
  ftpClient.binary();
 }

 //上傳文件;并返回上傳文件的信息
 private String upload(String filename) throws Exception {
  TelnetOutputStream os = null;
  FileInputStream is = null;
  try {
   //"upftpfile"用ftp上傳后的新文件名
   os = ftpClient.put("upftpfile");
   java.io.File file_in = new java.io.File(filename);
   if (file_in.length()==0) {
    return "上傳文件為空!";
   }
   is = new FileInputStream(file_in);
   byte[] bytes = new byte[1024];
   int c;
   while ((c = is.read(bytes)) != -1) {
    os.write(bytes, 0, c);
   }
  } finally {
   if (is != null) {
    is.close();
   }
   if (os != null) {
    os.close();
   }
  }
  return "上傳文件成功!";
 }

}

文件2:upftp.htm(前臺操作頁面)內容如下:

 

文件3:view_inf.jsp(信息提示頁面)和upftp.htm一樣放在context的根目錄下,

內容如下:

<%@page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
th
{
background-color: #4455aa;
color: white;
font-size: 14px;
font-weight:bold;
}
td.TableBody1
{
background-color: #FFFFF0;
color: white;
font-size: 14px;
font-weight:bold;
font-color: red;
}
.tableBorder1
{
width:97%;
border: 1px;
background-color: #6595D6;
}
</style>
</head>
<body>
<%String inf = (String) request.getAttribute("inf");
   if (inf == null) {
    inf = request.getParameter("inf");
   }%>
<table class="tableborder1" style="width: 75%;" align="center"
 cellpadding="3" cellspacing="1">
 <tbody>
  <tr align="center">
   <th colspan="2" height="25" width="100%">信 息 提 示:</th>
  </tr>
  <tr align="center">
   <td class="tablebody1" colspan="2" width="100%" height="200"><%=inf%></td>
  </tr>
  <tr align="center">
   <td><input name="back" value="返 回" onclick="history.back();"
    type="button" /></td>
  </tr>
 </tbody>
</table>
</body></html>

文件4:web.xml(j2ee的備置文件)放在WEB-INF目錄下,

內容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">
 <!--Servlet name-->
 <servlet>
  <servlet-name>MainCtrl</servlet-name>
  <servlet-class>MainCtrl</servlet-class>
 </servlet>
 <!--Servlet mapping-->
 <servlet-mapping>
  <servlet-name>MainCtrl</servlet-name>
  <url-pattern>/MainCtrl</url-pattern>
 </servlet-mapping>
</web-app>