java读取服务器上的文件夹里的所有文件 我这里有个网址: http://10.10.10.10 上面有很多文件。我现在需要用javaSE 读取里面的所有文件将他们下载到本地。请大家附代码!只给回答最全面的人的全分!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try { java.io.InputStream inputStream; java.net.URL url = new java.net.URL(surl); java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url .openConnection(); connection.connect(); inputStream = connection.getInputStream(); connection.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } 自己加工下inputStream输入流写入文件你总会吧。 我的url地址里是服务器里的一个文件夹,不是文件。看清敢不敢? 用递归得到所有的文件夹,再用HTTPClient下载吧 项目中的工具类,你自己看看,有上传也有下载import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPFileFilter;import org.apache.commons.net.ftp.FTPReply;import com.baosight.iplat4j.util.NumberUtils;/** * * Project:ieps * <p>FTP文件上传下载工具类</p> * * Create On 2011-2-22 下午01:37:25 * * @author <a href="mailto:[email protected]">dinglimin</a> * @version 1.0 */public class FtpUtil { public static String url; public static int port; public static String user; public static String pwd; public static void init() throws Exception{ System.out.println("初始化FTP参数..."); try{ url =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.ip"); port =NumberUtils.toint(ConfigFileRead.readerConfig("FilePath", "ftp.Remote.port")); user =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.user"); pwd = ConfigFileRead.readerConfig("FilePath", "ftp.Remote.password"); }catch(Exception e){ e.printStackTrace(); throw new Exception("connet remote ftp faild... ..."); } } public static void connectServer(FTPClient ftpClient){ try{ int reply; ftpClient.connect(url, port);// 连接FTP服务器 // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftpClient.login(user, pwd);// 登录 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); } }catch(Exception e){ e.printStackTrace(); } } /** * Description: 向FTP服务器上传文件 * * @param url * FTP服务器hostname * @param port * FTP服务器端口 * @param username * FTP登录账号 * @param password * FTP登录密码 * @param path * FTP服务器保存目录 * @param filename * 上传到FTP服务器上的文件名 * @param input * 输入流 * @return 成功返回true,否则返回false * @throws Exception */ public static boolean uploadFile(String path, String filename, InputStream input) throws Exception { boolean success = false; FTPClient ftpClient= new FTPClient(); try { connectServer(ftpClient); ftpClient.changeWorkingDirectory(path); ftpClient.storeFile(filename, input); input.close(); ftpClient.logout(); success = true; } catch (IOException e) { e.printStackTrace(); throw new Exception("文件上传过程中发生异常!"+e.getMessage()); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return success; } /** * Description: 从FTP服务器下载文件 * * @param url * FTP服务器hostname * @param port * FTP服务器端口 * @param username * FTP登录账号 * @param password * FTP登录密码 * @param remotePath * FTP服务器上的相对路径 * @param fileName * 要下载的文件名 * @param localPath * 下载后保存到本地的路径 * @return * @throws Exception */ public static boolean downFile(String remotePath, String ftpName, String fileName, HttpServletResponse response) throws Exception { boolean success = false; OutputStream out = null; FTPClient ftpClient= new FTPClient(); try { connectServer(ftpClient); ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftpClient.listFiles(remotePath); for (int i = 0; i < fs.length; i++) { if (fs[i].getName().equals(ftpName)) { // ByteArrayOutputStream os = new ByteArrayOutputStream(); File f = new File(ftpName); FileOutputStream fos = new FileOutputStream(f); boolean bf = ftpClient.retrieveFile(ftpName, fos); if (bf) { FileInputStream fis = new FileInputStream(ftpName); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch; while((ch=fis.read())!=-1) { bos.write(ch); } response.reset(); response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;" + " filename="+ new String(fileName.getBytes(), "ISO-8859-1")); byte[] bt=bos.toByteArray(); out = response.getOutputStream(); out.write(bt, 0, bt.length); fis.close(); } fos.close(); out.flush(); out.close(); } } ftpClient.logout(); success = true; } catch (IOException e) { e.printStackTrace(); throw new Exception("文件下载过程中发生异常!"+e.getMessage()); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } return success; } /** * 删除服务器上的文件 * @param remotePath * @param dbFileName * @return */ public static boolean deleteRemoteFiles(String remotePath,String[] dbFileName,String date){ System.out.println("::: DELETE REMOTE FTP FILE BEGIN :::"); boolean success = false; FTPClient ftpClient= new FTPClient(); try { connectServer(ftpClient); ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 final String testDate = date; //ftpClient.listFiles(pathname, filter) if(!StringUtil.isArray(dbFileName)){ return true; } FTPFile[] fs = ftpClient.listFiles(remotePath, new FTPFileFilter() { public boolean accept(FTPFile file) { String fileName = file.getName(); boolean ret = fileName.startsWith(testDate); return ret; } }); for (int i = 0; i < fs.length; i++) { boolean temp=false; //System.out.println("服务器满足条件文件:"+fs[i].getName()); for (int j = 0; j < dbFileName.length; j++) { if (dbFileName[j].equals(fs[i].getName())) { temp=true; break; } } if(!temp){ //System.out.println("文件["+fs[i].getName()+"]在数据库中不存在,删除..."); ftpClient.deleteFile(remotePath+"/"+fs[i].getName()); } } ftpClient.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } System.out.println("::: DELETE REMOTE FTP FILE END :::"); return success; } public static void main(String[] args) { String[] files={"20110222113447353.txt"}; try { FtpUtil.init(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } FtpUtil.deleteRemoteFiles("/cgftp/Attachment",files, "20110222"); }} 昨天心情不好,开贴向你道歉http://topic.csdn.net/u/20110820/10/6e4101f7-98e2-422e-83c4-8d75881ae26c.html File(URI uri)这个方法,可以传一个uri,然后File[] files = File.listFiles();然后循环这个数组下载。 如何读取这个文件(菜鸟级问题) 批处理命令 求助!! 请教一个for()的问题 JAVA中有没有类似C++中eof文件结尾的标志? Re:构造url对象的参数如何写 100分请教:怎么在有包类下调用无包类 学jsp2个月了.老师要做项目.新闻发布系统.有看过的提供本电子书最好有系统分析的.我还没方向呢..帮忙啊 请问JTable怎样删除某一行的数据?送分! java中关于引用与值传递的问题 请问j2ee,j2se,j2me具体指什么玩意啊? 请教各位,帮忙解决这个return的问题 如何正确的读取文件里数据?
try {
java.io.InputStream inputStream;
java.net.URL url = new java.net.URL(surl);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url
.openConnection();
connection.connect();
inputStream = connection.getInputStream();
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
inputStream输入流写入文件你总会吧。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPReply;import com.baosight.iplat4j.util.NumberUtils;
/**
*
* Project:ieps
* <p>FTP文件上传下载工具类</p>
*
* Create On 2011-2-22 下午01:37:25
*
* @author <a href="mailto:[email protected]">dinglimin</a>
* @version 1.0
*/
public class FtpUtil {
public static String url; public static int port; public static String user; public static String pwd; public static void init() throws Exception{
System.out.println("初始化FTP参数...");
try{
url =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.ip");
port =NumberUtils.toint(ConfigFileRead.readerConfig("FilePath", "ftp.Remote.port"));
user =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.user");
pwd = ConfigFileRead.readerConfig("FilePath", "ftp.Remote.password");
}catch(Exception e){
e.printStackTrace();
throw new Exception("connet remote ftp faild... ...");
}
} public static void connectServer(FTPClient ftpClient){ try{
int reply;
ftpClient.connect(url, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftpClient.login(user, pwd);// 登录
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
} /**
* Description: 向FTP服务器上传文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
* @throws Exception
*/
public static boolean uploadFile(String path, String filename, InputStream input) throws Exception { boolean success = false;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient); ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(filename, input);
input.close();
ftpClient.logout();
success = true; } catch (IOException e) {
e.printStackTrace();
throw new Exception("文件上传过程中发生异常!"+e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
} /**
* Description: 从FTP服务器下载文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return
* @throws Exception
*/
public static boolean downFile(String remotePath, String ftpName,
String fileName, HttpServletResponse response) throws Exception { boolean success = false;
OutputStream out = null;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient);
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftpClient.listFiles(remotePath);
for (int i = 0; i < fs.length; i++) {
if (fs[i].getName().equals(ftpName)) { // ByteArrayOutputStream os = new ByteArrayOutputStream();
File f = new File(ftpName);
FileOutputStream fos = new FileOutputStream(f);
boolean bf = ftpClient.retrieveFile(ftpName, fos);
if (bf) {
FileInputStream fis = new FileInputStream(ftpName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while((ch=fis.read())!=-1) {
bos.write(ch);
} response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;"
+ " filename="+ new String(fileName.getBytes(), "ISO-8859-1"));
byte[] bt=bos.toByteArray();
out = response.getOutputStream();
out.write(bt, 0, bt.length);
fis.close();
}
fos.close();
out.flush();
out.close();
}
} ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
throw new Exception("文件下载过程中发生异常!"+e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
} /**
* 删除服务器上的文件
* @param remotePath
* @param dbFileName
* @return
*/
public static boolean deleteRemoteFiles(String remotePath,String[] dbFileName,String date){
System.out.println("::: DELETE REMOTE FTP FILE BEGIN :::");
boolean success = false;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient);
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 final String testDate = date; //ftpClient.listFiles(pathname, filter)
if(!StringUtil.isArray(dbFileName)){
return true;
} FTPFile[] fs = ftpClient.listFiles(remotePath, new FTPFileFilter() {
public boolean accept(FTPFile file) {
String fileName = file.getName();
boolean ret = fileName.startsWith(testDate);
return ret;
}
}); for (int i = 0; i < fs.length; i++) {
boolean temp=false;
//System.out.println("服务器满足条件文件:"+fs[i].getName());
for (int j = 0; j < dbFileName.length; j++) {
if (dbFileName[j].equals(fs[i].getName())) {
temp=true;
break;
}
}
if(!temp){
//System.out.println("文件["+fs[i].getName()+"]在数据库中不存在,删除...");
ftpClient.deleteFile(remotePath+"/"+fs[i].getName());
}
}
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
System.out.println("::: DELETE REMOTE FTP FILE END :::");
return success;
} public static void main(String[] args) {
String[] files={"20110222113447353.txt"};
try {
FtpUtil.init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FtpUtil.deleteRemoteFiles("/cgftp/Attachment",files, "20110222");
}}
http://topic.csdn.net/u/20110820/10/6e4101f7-98e2-422e-83c4-8d75881ae26c.html