赞
踩
前端主要部分
<script type="text/javascript">
</script>
<body>
<a id = 'cardImg' href= 'javascript:void(0);'
url='图片相对路径'
>下载</a>
<a id = 'cardImgBack' href= 'javascript:void(0);'
url='图片相对路径'
>下载</a>
</body>
服务端
getImg.action实现 主要部分
public String execute() throws Exception {
ServletOutputStream out = null;
String fileServer = “可以访问图片的域名或路径”;
String imgPath = request_.getParameter("path");
try {
//获取图片存放路径
String imgUrl = fileServer + imgPath;
log.info("图片真实路径: "+ imgUrl);
URL url = new URL(imgUrl);
SslUtils.ignoreSsl();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream input = conn.getInputStream();//通过输入流获取图片数据
response_.setContentType("multipart/form-data");
out = response_.getOutputStream();
//读取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = input.read(buffer)) != -1){
out.write(buffer,0,len);
}
out.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
if (out != null) {
out.close();
}
}
return null;
}
struts.xml 配置
<action name="getImg" class="action位置">
</action>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。