java读取服务器上的文件夹里的所有文件
我这里有个网址: http://10.10.10.10 上面有很多文件。我现在需要用javaSE 读取里面的所有文件将他们下载到本地。请大家附代码!只给回答最全面的人的全分!!!!!! --------------------编程问答-------------------- HttpURLConnection --------------------编程问答-------------------- http://down.chinaz.com/soft/25826.htm --------------------编程问答----------------------------------------编程问答-------------------- 自己加工下
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:dlm104@126.com">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 --------------------编程问答--------------------
很多小人都只会阿谀奉承,你是真男人。鉴定完毕~~ --------------------编程问答-------------------- LZ..你霸气侧漏 --------------------编程问答-------------------- 哈哈 烈女啊 艹 --------------------编程问答-------------------- File(URI uri)这个方法,可以传一个uri,然后File[] files = File.listFiles();然后循环这个数组下载。
--------------------编程问答--------------------
得到的是一个空值!谢谢~~~~~~~~~~
补充:Java , Java SE