JAVA开发FTP客户端如何实现上传速率,上传时间? 我用JAVA实现FTP上传请教各位如何获得上传速率,上传时间呀? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 等了几天居然没有人回答。还是老毛说的对:自己动手,丰衣足食。通过自己的努力解决了,看来万事还得靠自己。现在我贴出来大家共享。想了解的更详细请加群咨询:6040086 或者加我QQ:124029020<%@ page contentType="text/html; charset=gb2312" language="java"%><%@ page import="java.util.*"%><%@ page import="ahxu.commons.upload.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="java.util.regex.*"%><%@ page import="java.io.*"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>上传文件接收</title></head><body><%/** * * <p>Title: 实现上传</p> * <p>Description: 实现文件的上传功能</p> * <p>Copyright: Copyright (c) 2007</p> * <p>Company: 联合信息</p> * @author 郭新伟 * @date 04月17日 * @version 1.0 */String filepath=request.getParameter("filepath")==null?"":new String(request.getParameter("filepath").getBytes("ISO-8859-1"));System.out.println("你设置的路径为:"+filepath);String uploadtemppath = "d:\\temp";////临时存储的数据的路径java.io.File dd=new java.io.File(uploadtemppath); if(!dd.exists()){ dd.mkdir(); out.println(uploadtemppath+"目录不存在,已建立"); }String uploadpath = "d:\\upload\\"; //保存上传的文件到指定的目录java.io.File ddd=new java.io.File(uploadpath); if(!ddd.exists()){ ddd.mkdir(); out.println(uploadpath+"目录不存在,已建立"); }//org.apache.commons.fileupload.List fileItemList=null;//DiskFileUpload fu = new DiskFileUpload();//初使化使用原有apache的uploadahxu.commons.upload.DiskFileUploadEx fu = new ahxu.commons.upload.DiskFileUploadEx();//初使化ahxu扩展后的组件fu.setSizeMax(500*1024*1024);//原有apache的upload:设置允许上传的最大值fu.setSizeThreshold(1*1024*1024);// 设置最多只允许在内存中存储的数据,单位:字节fu.setRepositoryPath(uploadtemppath); //临时存储的数据的路径fu.setAllowFiles(".txt;.TXT;.doc;.jpg;.JPG;.PNG;.png;.gif;.GIF;.rm;.rmvb;.htm;.html;.xml;.amv;.3gp;.MID;.mid;.midi;.rar;.RAR;.zip");//ahxu扩展后:设置允许的上传文件类型 String regExp=".+\\\\(.+)$"; Pattern p = Pattern.compile(regExp);try{ //fileItemList=fu.parseRequest(request);//使用原有apache的upload:由request接收上传文件 fileItemList=fu.parseRequestEx(request);//ahxu扩展后的:接收上传文件 //System.out.println("aaaaaaaa"); Iterator fileItemListIte=fileItemList.iterator(); while(fileItemListIte.hasNext()) { FileItem item=(FileItem)fileItemListIte.next(); if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if((name==null||name.equals("")) && size==0) continue; Matcher m = p.matcher(name); boolean result = m.find(); if (result){ try{ //保存上传的文件到指定的目录 //在下文中上传文件至数据库时,将对这里改写 //item.write(new File("d:\\upload\\" + m.group(1))); item.write(new File(uploadpath + m.group(1))); out.print(name+" "+"文件。<br>"); out.print("共"+size+"字节。<br>"); } catch(Exception e){ out.println(e); } }else{ throw new IOException("fail to upload"); } } } out.println("上传成功<br>"); out.println("<a href='./upload.jsp'>返回</a>");}catch (Exception e){ out.println("上传失败<br>"); out.println(e.getMessage()); /*显示不被允许上传的文件列表,原有apache的upload无此部分 begin*/ if(e instanceof DiskFileUploadEx.InvalidFileUploadException){ out.println("<p>以下文件不被允许:</p>"); Iterator unAllowFileS=((DiskFileUploadEx.InvalidFileUploadException)e).getInvalidFileList().iterator(); while(unAllowFileS.hasNext()){ out.println((String)unAllowFileS.next()+"<br>"); } /*显示不被允许上传的文件列表,原有apache的upload无此部分 end */ }}finally{Thread.sleep(1000);fu.dispose();//ahxu扩展后的组件:接收上传文件后的清理工作,原有apache组件没有此过程}%></body></html> 我顶 这..这和FTP有什么关系大哥 那你了解的FTP上传是?开发一个客户端?用微软自带的FTP服务器?通过hostnam port username password链接到服务器?public static boolean connect(String sHost, String user,String password ,String sDir){try{m_client = new FtpClient("localhost");m_client.login("administrator", "gxw1010");m_client.cd("e");m_client.binary();}catch (Exception ex){ ex.printStackTrace();return false;}return true;}public static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile){if (m_sLocalFile.length()==0){return false;}byte[] buffer = new byte[10240];try{File f = new File(m_sLocalFile);int size = (int)f.length();FileInputStream in = new FileInputStream(m_sLocalFile);OutputStream out = m_client.put(m_sHostFile);int counter = 0;while(true){int bytes = in.read(buffer);if (bytes < 0)break;out.write(buffer, 0, bytes);counter += bytes;}//System.out.println(counter);out.close();in.close();}catch (Exception ex){ ex.printStackTrace();//gxwreturn false;}return true;}这种方法我还没有想到如何得到上传速率。想到以后在帖出来供大家参考 ftp上传用的是ftp协议,楼主,你的apach fileupload应该用的是http协议吧 HTTP协议是基于SmartUpload.jar包的有组件上传commons-io-1.3.1.jar是apach的 FTP上传 不好意思,我没有做这个,只是提出自己的想法。这个想法是我在看j-ftp的源码时想到的,它虽然没有实现速率,但是它记下了每段时间所上下载的字节数,那这样要得到速率也不难了,说得不对请指正 java中print()和println()有什么区别? 高手抢分了~~怎样编写一个多功能计算器~~要求功能全部实现..>>?? 头大了。。。 界面按扭大小相同的设定? java下那种可视化编程工具最好??? JAVA求字符串的子串,谢谢! 关于jbuilder的简单问题 储存目录结构该用什么数据结构? The expression of type List needs unchecked conversion to conform to List<Guest> 多线程并发时数据结果混乱 关于servlet! 在JTabbedPane中如何很好地用setTitleAt动态设置Tab上文字?
还是老毛说的对:自己动手,丰衣足食。
通过自己的努力解决了,看来万事还得靠自己。现在我贴出来大家共享。想了解的更详细请加群咨询:6040086 或者加我QQ:124029020
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="ahxu.commons.upload.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上传文件接收</title>
</head><body>
<%
/**
*
* <p>Title: 实现上传</p>
* <p>Description: 实现文件的上传功能</p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: 联合信息</p>
* @author 郭新伟
* @date 04月17日
* @version 1.0
*/
String filepath=request.getParameter("filepath")==null?"":new String(request.getParameter("filepath").getBytes("ISO-8859-1"));
System.out.println("你设置的路径为:"+filepath);String uploadtemppath = "d:\\temp";////临时存储的数据的路径
java.io.File dd=new java.io.File(uploadtemppath);
if(!dd.exists()){
dd.mkdir();
out.println(uploadtemppath+"目录不存在,已建立");
}
String uploadpath = "d:\\upload\\"; //保存上传的文件到指定的目录
java.io.File ddd=new java.io.File(uploadpath);
if(!ddd.exists()){
ddd.mkdir();
out.println(uploadpath+"目录不存在,已建立");
}//org.apache.commons.fileupload.
List fileItemList=null;
//DiskFileUpload fu = new DiskFileUpload();//初使化使用原有apache的upload
ahxu.commons.upload.DiskFileUploadEx fu = new ahxu.commons.upload.DiskFileUploadEx();//初使化ahxu扩展后的组件
fu.setSizeMax(500*1024*1024);//原有apache的upload:设置允许上传的最大值
fu.setSizeThreshold(1*1024*1024);// 设置最多只允许在内存中存储的数据,单位:字节
fu.setRepositoryPath(uploadtemppath); //临时存储的数据的路径
fu.setAllowFiles(".txt;.TXT;.doc;.jpg;.JPG;.PNG;.png;.gif;.GIF;.rm;.rmvb;.htm;.html;.xml;.amv;.3gp;.MID;.mid;.midi;.rar;.RAR;.zip");//ahxu扩展后:设置允许的上传文件类型
String regExp=".+\\\\(.+)$";
Pattern p = Pattern.compile(regExp);
try{ //fileItemList=fu.parseRequest(request);//使用原有apache的upload:由request接收上传文件
fileItemList=fu.parseRequestEx(request);//ahxu扩展后的:接收上传文件
//System.out.println("aaaaaaaa");
Iterator fileItemListIte=fileItemList.iterator();
while(fileItemListIte.hasNext())
{
FileItem item=(FileItem)fileItemListIte.next();
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
try{
//保存上传的文件到指定的目录
//在下文中上传文件至数据库时,将对这里改写
//item.write(new File("d:\\upload\\" + m.group(1)));
item.write(new File(uploadpath + m.group(1)));
out.print(name+" "+"文件。<br>");
out.print("共"+size+"字节。<br>");
}
catch(Exception e){
out.println(e);
}
}else{
throw new IOException("fail to upload");
}
}
}
out.println("上传成功<br>");
out.println("<a href='./upload.jsp'>返回</a>");
}catch (Exception e){
out.println("上传失败<br>");
out.println(e.getMessage());
/*显示不被允许上传的文件列表,原有apache的upload无此部分 begin*/
if(e instanceof DiskFileUploadEx.InvalidFileUploadException){
out.println("<p>以下文件不被允许:</p>");
Iterator unAllowFileS=((DiskFileUploadEx.InvalidFileUploadException)e).getInvalidFileList().iterator();
while(unAllowFileS.hasNext()){
out.println((String)unAllowFileS.next()+"<br>");
}
/*显示不被允许上传的文件列表,原有apache的upload无此部分 end */
}}
finally{
Thread.sleep(1000);
fu.dispose();//ahxu扩展后的组件:接收上传文件后的清理工作,原有apache组件没有此过程
}
%>
</body>
</html>
开发一个客户端?用微软自带的FTP服务器?
通过hostnam port username password链接到服务器?
public static boolean connect(String sHost, String user,String password ,String sDir)
{
try
{
m_client = new FtpClient("localhost");
m_client.login("administrator", "gxw1010");
m_client.cd("e");
m_client.binary();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}return true;
}public static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile)
{
if (m_sLocalFile.length()==0)
{
return false;
}byte[] buffer = new byte[10240];try
{
File f = new File(m_sLocalFile);
int size = (int)f.length();
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(m_sHostFile);
int counter = 0;
while(true)
{
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
}
//System.out.println(counter);
out.close();
in.close();
}
catch (Exception ex)
{
ex.printStackTrace();//gxw
return false;
}return true;
}
这种方法我还没有想到如何得到上传速率。
想到以后在帖出来供大家参考
commons-io-1.3.1.jar是apach的 FTP上传