当前位置:首页 » 文件管理 » ftpjava下载文件

ftpjava下载文件

发布时间: 2022-12-27 11:51:27

‘壹’ java如何实现txt下载 就是从远程ftp服务器上直接把TXT文本下载下来!

strUrl为文件地址,fileName为文件在本地的保存路径,试试吧~

public static void writeFile(String strUrl, String fileName) {
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream( fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];

while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
System.out.println("下载成功:"+strUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

‘贰’ java 使用ftp 下载文件在windows环境下正常,在linux下载不了

是不是文件的读写权限问题,linux的权限控制比windows严格的多

‘叁’ 看了一段java代码是从FTP上下载文件,ftpClient.setBufferSize()这个是什么用处,要怎么使用它

缓冲区大小,等从ftp下载的数据存储到缓冲区,等缓冲区满了,进行磁盘读写。

‘肆’ java ftp下载

这个和ftp没有太大关系,只是一个普通的下载,java连接ftp服务器传输文件是需要提供ip 端口号,用户名密码 路径的,这个只是一个静态资源,用这个就可以,支持断点续传

这边用到了apachecommons-httpclient-3.1包

importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;

importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
publicclassDownloadTool{
publicstaticvoidmain(String[]args)throwsClientProtocolException,IOException{
Stringurl="
[阳光电影
].神探驾到.BD.720p.国粤双语中字.mkv";
StringdownFile="d:\aaa.mkv";//本地存放路径
LongnetFileLenght=getNetFileSize(url);
LonglocalFileLenght=getLocalFileSize(downFile);

if(localFileLenght>=netFileLenght){
System.out.println("已下载完成");
return;
}

System.out.println("netFileLenght:"+netFileLenght+"localFileLenght:"+localFileLenght);
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);

finalHttpGethttpGet=newHttpGet(url);
httpGet.addHeader("Range","bytes="+localFileLenght+"-");

finalHttpResponseresponse=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
System.out.println(code);

if(entity!=null&&code<400){

Filefile=newFile(downFile);
=newRandomAccessFile(file,"rw");
randomAccessFile.seek(localFileLenght);

InputStreaminputStream=entity.getContent();
intb=0;
finalbytebuffer[]=newbyte[1024];
while((b=inputStream.read(buffer))!=-1){
randomAccessFile.write(buffer,0,b);
}

randomAccessFile.close();
inputStream.close();
httpClient.getConnectionManager().shutdown();
System.out.println("下载完成");
}
}

(StringfileName){
Filefile=newFile(fileName);
returnfile.length();
}

(Stringurl){
Longcount=-1L;
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);

finalHttpGethttpGet=newHttpGet(url);
HttpResponseresponse=null;
try{
response=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
if(entity!=null&&code==200){
count=entity.getContentLength();
}
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();
}
returncount;
}
}

‘伍’ java ftp批量下载异常

我之前也遇到过这样的事,通过FTP获取文件的二进制流有限制,获取第二个流的时候需要断掉链接后再重新连接服务器读取流

‘陆’ java ftp连接一次下载多个文件

/**
*参考例子如下:
*@paramnamelist下载的文件列表
*@parampath下载路径
*@paramzipname压缩文件名称
*/
publicvoidzipDownloadFile(HttpServletResponseresponse,List<string>namelist,Stringpath,Stringzipname){
byte[]buf=newbyte[1024];

try{

//本地保存设置
response.addHeader("Content-Disposition","attachment;filename="
+URLEncoder.encode(zipname,sysEncoding)+".zip");

response.setContentType("application/x-zip-compressed");

//向本地写文件
ServletOutputStreamsos=response.getOutputStream();


ZipOutputStreamzipOut=newZipOutputStream(newBufferedOutputStream(sos));


for(Stringname:namelist){

ZipEntryentry=newZipEntry(name);
zipOut.putNextEntry(entry);

InputStreambis=this.getStream(path,name);
if(bis!=null){
intreadLen=-1;

while((readLen=bis.read(buf,0,1024))!=-1){
zipOut.write(buf,0,readLen);
}

bis.close();
}
}

zipOut.close();


}catch(Exceptione){

e.printStackTrace();
}

}</string>

‘柒’ ftp java 下载的时候怎么保存到指定位置

通过工具类来实现本地路径定义和下载即可。

import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class CCFCCBFTP {

/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}

}

/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try {
Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
CMBCUtil.CMBCLOGINPASSWORD);

FTPFile[] fs;

ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下载文件开始。");
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下载文件结束:"+localFilePath);
}
}
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}

// 调用样例:
public static void main(String[] args) {
try {

// 明文文件路径
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路径
String secretFilePath = "req_20150204_00134.txt";
fileDownloadByFtp("D://123.zip","123.zip","req/20150228");
} catch (Exception e) {
e.printStackTrace();
}
}

}

‘捌’ java FTP下载文件在代码中如何实现知道下载完成

(KmConfigkmConfig,StringfileName,StringclientFileName,OutputStreamoutputStream){
try{
StringftpHost=kmConfig.getFtpHost();
intport=kmConfig.getFtpPort();
StringuserName=kmConfig.getFtpUser();
StringpassWord=kmConfig.getFtpPassword();
Stringpath=kmConfig.getFtpPath();
FtpClientftpClient=newFtpClient(ftpHost,port);//ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。
ftpClient.login(userName,passWord);//userName、passWord分别为FTP服务器的登陆用户名和密码
ftpClient.binary();
ftpClient.cd(path);//path为FTP服务器上保存上传文件的路径。
try{
TelnetInputStreamin=ftpClient.get(fileName);
byte[]bytes=newbyte[1024];
intcnt=0;
while((cnt=in.read(bytes,0,bytes.length))!=-1){
outputStream.write(bytes,0,cnt);
}
//##############################################
//这里文件就已经下载完了,自己理解一下
//#############################################

outputStream.close();
in.close();
}catch(Exceptione){
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
}catch(Exceptione){
System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");
}
}

‘玖’ 我在用JAVA写FTP下载的时候出现这个问题

估计是C:\Documents and Settings\Administrator\Application Data\这个文件夹的问题,应该是windows系统指定用来存放某些文件的目录。那你就不要非得放在这个目录好了,不要在一棵树上吊死。

‘拾’ java 下载异地FTP中的zip文件

好像需要一个支持jar包把,把ftp4j的下载地址贴出来

热点内容
压缩和r 发布:2025-08-22 22:30:42 浏览:15
sql获取小时 发布:2025-08-22 22:10:58 浏览:670
大同网通dns服务器地址 发布:2025-08-22 22:02:22 浏览:591
javarsa的是 发布:2025-08-22 21:51:58 浏览:712
ftp解析域名解析 发布:2025-08-22 21:48:30 浏览:538
与佛论道加密 发布:2025-08-22 21:41:42 浏览:345
cs架构语言 发布:2025-08-22 21:34:35 浏览:883
安防监控存储 发布:2025-08-22 21:20:38 浏览:800
java高级培训哪里好 发布:2025-08-22 21:16:56 浏览:170
套杯算法 发布:2025-08-22 21:15:20 浏览:253