前几天写过一编"关于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(前台操作页面)内容如下:
<html><body> <form method="post" action="/upftp/MainCtrl"> <!-- C:\\Downloads\\setup_kubao.exe客户端真实文件路径 --> <input type="text" name="file_name" size="38" value="C:\\Downloads\\setup_kubao.exe" /> <input type="submit" /> </form></body> </html>
文件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="返 回" οnclick="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>