java 下载功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 下载功能相关的知识,希望对你有一定的参考价值。

参考技术A 下载功能实际上就是将远程服务器的文件流通过ftp功能转换为本地文件流进行存储。举例:
/**
*下载并解压文件
*
* @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);

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);




备注:只需要修改 ftpClient.connect方法中的用户名和密码即可进行远程服务器连接下载,具体的根据实际情况修改即可。

java实现下载附件功能


前台js方法:

//下载为Excel
function downResult()

var url = $ctx/downResult.do;
var fileName = "稽核结果.xlsx";
var form = $("<form></form>").attr("action", url).attr("method", "post");
form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", fileName));
form.appendTo(body).submit().remove();

后台controller方法(这里下载的类型是Excel,下载其他文件需要修改类型):

@ResponseBody
@RequestMapping(value="downResult.do")
public ModelAndView downResult(HttpServletRequest request,
HttpServletResponse response)throws Exception

URL url = getClass().getClassLoader().getResource("auditResult.xls");
String path = url.getPath();
//downloadFile(request,response, path, "auditResult.xlsx");
down(request,response, path, "auditResult.xlsx");

return null;


private void down(HttpServletRequest request, HttpServletResponse response, String path,String fileName) throws Exception
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
try
String agent = request.getHeader("User-Agent");
boolean isMSIE = (agent != null && (agent.indexOf("MSIE") != -1 || agent.indexOf("Trident") != -1));

if (isMSIE)
response.setHeader("Content-disposition",
"attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF8"));
else
response.setHeader("Content-disposition",
"attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));

response.setContentType("application/x-xls");


bis = new java.io.BufferedInputStream(new java.io.FileInputStream(path));
bos = new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
bos.write(buff, 0, bytesRead);

catch(Exception e)
/*operResult.setResult("fail");
operResult.setMsg("操作失败:"+e.getMessage());*/
e.printStackTrace();
finally
if (bis != null)
bis.close();
if (bos != null)
bos.close();

 

以上是关于java 下载功能的主要内容,如果未能解决你的问题,请参考以下文章

java实现下载附件功能

手写一个微型下载资源网站Java实现用户注册登陆下载功能

Java实现FTP上传下载功能

JAVA文件下载功能问题解决日志

java web文件下载功能实现

Java单线程文件下载,支持断点续传功能