1:用http协议上传更适合web编程的方便;传小于1M文件速度要比用ftp协议上传文件略快。安全性好;不像ftp那样;必须要启动一个ftp服务才行。2:用ftp协议上传文件大于1M的文件速度比http快;文件越大;上传的速度就比http上传快的倍数越大。而且用java编写程序;ftp比http方便。好,废话少说;我们先搭建一个实例来理性认识一下用java编写ftp上传文件的技术。
首先在本机启动一个ftp服务,ftp的用户:"FrzccyChen" 密码:"123456789";
文件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.File;
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 {
try {
// 连接ftp服务器
connectServer("10.10.7.124", "FrzccyChen", "123456789", "\\");
// 上传文件;并返回上传文件的信息
if (upload(req.getParameter("file_name")) == null) {
req.setAttribute("inf", "上传文件失败");
}
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 s = null;
FileInputStream is = null;
try {
File file = new File(filename);
String tempFile = file.getName();//获取上传文件的文件名
os = ftpClient.put(tempFile);
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.jsp(前台操作页面)内容如下:
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<title>FTP上传文件</title>
</head>
<body>
<form. method="post" action="<%=request.getContextPath()%>/MainCtrl">
<input type="file" name="file_name" size="38"/>
<input type="submit" value="上传" />
</form>
</body>
</html>
文件3:view_inf.jsp(信息提示页面)和upftp.jsp一样放在context的根目录下
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
<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>
<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 colspan="2" width="100%" height="200">
<font color="red">${inf}</font>
</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="utf-8"?>
<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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!--Servlet name-->
<servlet>
<servlet-name>MainCtrl</servlet-name>
<servlet-class>com.test.ftp.MainCtrl</servlet-class>
</servlet>
<!--Servlet mapping-->
<servlet-mapping>
<servlet-name>MainCtrl</servlet-name>
<url-pattern>/MainCtrl</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Encoding</filter-name>
<filter-class>com.test.ftp.Encoding</filter-class>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
PS:如果要上传带中文名称的文件名,需要改动FTP服务的设置.具体如下:
1、打开SERV-U---导航---限制和设置---FTP设置---找到OPTS UTF8 命令----编辑----选择禁用命令---然后确认
2、再选择全局属性-----高级选项---找到“对所有已收发的路径和文件名使用UTF-8编码”,把前面的勾去掉,确认。最后回到导航,用同样的方法设置“服务器限制和设置”,保存退出,就完成啦