专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java文件上传下载代码示例

Java文件上传下载代码示例

更新时间:2022-12-01 12:01:54 来源:赢咖4 浏览801次

文件上传和下载代码示例

[code lang=”java”]
package com.resource.util;
导入 java.io.BufferedInputStream;
导入 java.io.BufferedOutputStream;
导入java.io.文件;
导入 java.io.FileInputStream;
导入 java.io.FileOutputStream;
导入java.io.IOException;
导入 java.net.MalformedURLException;
导入java.net.URL;
导入 java.net.URLConnection;
/**
* 此类用于将文件上传到 FTP 服务器。
*
* @author Muthu
*/
public class FileUpload
{
/**
* 将文件上传到 FTP 服务器。
使用以下语法生成 FTP URL :
* ftp://user:password@host:port/filePath;type=i。
*
* @param ftpServer ,FTP 服务器地址(可选端口':portNumber')。
* @param user ,可选的用户名登录。
* @param password ,用户的可选密码。
* @param fileName ,FTP 服务器上的目标文件名(可选
* 前置相对路径,例如“myDir/myFile.txt”)。
* @param source ,要上传的源文件。
* @throws MalformedURLException,IOException 错误。
*/
public void upload( String ftpServer, String user, String password,
String文件名,文件源 ) 抛出 MalformedURLException,
IOException
{ // 检查身份验证否则假设其匿名访问。 if (user != null && password != null) { sb.append( user ); sb.append(':'); sb.append(密码); sb.append('@'); } sb.append(ftpServer); sb.append('/'); sb.append(文件名);
* 输入 ==> a=ASCII 模式, i=图像(二进制)模式, d= 文件目录
* 列表
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
尝试
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream( 源 ) );

诠释我;

// 逐字节读取直到流结束
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "输入不可用。" );
}
}
/**
* 从 FTP 服务器下载文件。
使用以下语法生成 FTP URL :
* ftp://user:password@host:port/filePath;type=i。
*
* @param ftpServer ,FTP 服务器地址(可选端口':portNumber')。
* @param user ,可选的用户名登录。
* @param password ,用户的可选密码。
* @param fileName ,要下载的文件的名称(前面有可选的
* 相对路径,例如 one/two/three.txt)。
* @param destination ,要保存的目标文件。
* @throws MalformedURLException,IOException 错误。
*/
public void download( String ftpServer, String user, String password,
String fileName, File destination ) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && destination != null)
{
StringBuffer sb = new StringBuffer( "ftp://" ); }
// 检查身份验证否则假设其匿名访问。
if (user != null && password != null)
{
sb.append( user );
sb.append(':');
sb.append(密码);
sb.append('@');
}
sb.append(ftpServer);
sb.append('/');
sb.append(文件名);
/*
* 类型 ==> a=ASCII 模式, i=图像(二进制)模式, d= 文件目录
* 列表
*/
sb.append( ";type=i" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

尝试

{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream(
destination.getName() ) );

诠释我;

while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
赶上(IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "输入不可用" );
}
}
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>