从网络Url中下载文件

1.pom配置
<? xml version ="1.0" encoding ="UTF-8" ?>
< project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion > 4.0.0 </ modelVersion >

< groupId > com.xfs.common.services </ groupId >
< artifactId > base-services </ artifactId >
< version > 0.0.1-SNAPSHOT </ version >
< packaging > jar </ packaging >

< name > base-services </ name >
< description > Demo project for Spring Boot </ description >

< parent >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-parent </ artifactId >
< version > 2.0.1.RELEASE </ version >
< relativePath /> <!-- lookup parent from repository -->
</ parent >

< properties >
< project.build.sourceEncoding > UTF-8 </ project.build.sourceEncoding >
< project.reporting.outputEncoding > UTF-8 </ project.reporting.outputEncoding >
< java.version > 1.8 </ java.version >
</ properties >

< dependencies >
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-web </ artifactId >
</ dependency >

< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-test </ artifactId >
< scope > test </ scope >
</ dependency >

< dependency >
< groupId > commons-fileupload </ groupId >
< artifactId > commons-fileupload </ artifactId >
< version > 1.2.2 </ version >
</ dependency >

< dependency >
< groupId > commons-io </ groupId >
< artifactId > commons-io </ artifactId >
< version > 2.4 </ version >
</ dependency >

< dependency >
< groupId > com.aliyun.oss </ groupId >
< artifactId > aliyun-sdk-oss </ artifactId >
< version > 2.8.1 </ version >
</ dependency >

<!-- jersey -->
< dependency >
< groupId > com.sun.jersey </ groupId >
< artifactId > jersey-server </ artifactId >
< version > 1.10 </ version >
</ dependency >
< dependency >
< groupId > com.sun.jersey </ groupId >
< artifactId > jersey-client </ artifactId >
< version > 1.10 </ version >
</ dependency >
< dependency >
< groupId > com.sun.jersey </ groupId >
< artifactId > jersey-core </ artifactId >
< version > 1.10 </ version >
</ dependency >

< dependency >
< groupId > log4j </ groupId >
< artifactId > log4j </ artifactId >
< version > 1.2.17 </ version >
</ dependency >
</ dependencies >

< build >
< plugins >
< plugin >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-maven-plugin </ artifactId >
</ plugin >
</ plugins >
</ build >


</ project >
2.后台代码
package com.xfs.common.services.baseservices.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.xfs.common.services.baseservices.util.AjaxObject;
import com.xfs.common.services.baseservices.util.OSSUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype. Controller ;
import org.springframework.web.bind.annotation. GetMapping ;
import org.springframework.web.bind.annotation. RequestMapping ;
import org.springframework.web.bind.annotation. ResponseBody ;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* 从网络 url 中下载文件
*
* @author :ZHANGPENGFEI
* @create 2018-06-19 11:28
**/
@Controller
@RequestMapping ( "/basicservice" )
public class DownloadFile {

private static Logger logger = Logger. getLogger (DownloadFile. class );

/**
* 从网络 Url 中下载文件到客户端
* @param urlStr
* @param fileName
* @throws IOException
*/
@GetMapping ( "/downLoadFromUrl" )
public static void downLoadFromUrl(String urlStr, String fileName,HttpServletResponse response,HttpServletRequest request) throws Exception{

// 如果用户没有传入新的文件名,默认生成一个
if ( null == fileName || "" .equals(fileName)){
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS" );
int num = ( int )(Math. random ()* 899 )+ 100 ;
fileName = sdf.format( new Date())+num;
}

// 处理文件名
int lastIndex = urlStr.lastIndexOf( "." );
String subfix = urlStr.substring(lastIndex);
fileName+=subfix;


URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// 设置超时间为 3
conn.setConnectTimeout( 3 * 1000 );
// 防止屏蔽程序抓取而返回 403 错误
conn.setRequestProperty( "User-Agent" , "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" );

// 得到输入流
InputStream inputStream = conn.getInputStream();

try {
// 缓冲文件输出流
BufferedOutputStream outputStream= new BufferedOutputStream(response.getOutputStream());
// 通知浏览器以附件形式下载
// response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
// 为防止 文件名出现乱码
response.setContentType( "application/doc" );
final String userAgent = request.getHeader( "USER-AGENT" );
if (StringUtils. contains (userAgent, "MSIE" )){ //IE 浏览器
fileName = URLEncoder. encode (fileName, "UTF-8" );
} else if (StringUtils. contains (userAgent, "Mozilla" )){ //google, 火狐浏览器
fileName = new String(fileName.getBytes(), "ISO8859-1" );
} else {
fileName = URLEncoder. encode (fileName, "UTF-8" ); // 其他浏览器
}
response.addHeader( "Content-Disposition" , "attachment;filename=" +fileName); // 这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开
byte [] car = new byte [ 1024 ];
int L;

while ((L = inputStream.read(car)) != - 1 ){
if (car. length != 0 ){
outputStream.write(car, 0 ,L);
}
}

if (outputStream!= null ){
outputStream.flush();
outputStream.close();
}


} catch (IOException e) {
e.printStackTrace();

} catch (OSSException e){

}



}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值