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){
}
}
}
|