java通过url下载⽂件并输出的⽅法实例如下所⽰:
controller:
@RequestMapping(value = "/{loanid}/{atmtType}")
java下载过程public void doGet(@PathVariable("loanid") String loanid,@PathVariable("atmtType") String atmtType,
HttpServletRequest req,HttpServletResponse response) {
Map<String,String> map = new HashMap<String,String>();
snmp属于哪一层协议map.put("loanid", loanid);
Map<String, String> urlByLoanid= new HashMap<String,String>();
// Map<String, String> urlByLoanid = zcmQueryInfoService.queryUrlByLoanid(map);
try {
if(urlByLoanid!=null){
String wjurl="10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_17101119371231615_7.pdf"; //String wjurl = ("url");
int i = wjurl.lastIndexOf("/");
String fileName = wjurl.substring(i+1);
URL url = new URL(wjurl);
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)");
//得到输⼊流
clothes cloth clothing的区别InputStream inputStream = InputStream();
//获取⾃⼰数组
byte[] bs = readInputStream(inputStream);
response.setContentType("application/octet-stream;charset=ISO8859-1");
BufferedOutputStream output = null;
BufferedInputStream input = null;
try {
output = new OutputStream());
// 中⽂⽂件名必须转码为 ISO8859-1,否则为乱码
String fileNameDown = new Bytes(), "ISO8859-1");
// 作为附件下载
response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
output.write(bs);
response.flushBuffer();
} catch (Exception e) {
<("Download log file error", e);
} // ⽤户可能取消了下载
fetchall的用法finally {
菜单栏标题栏状态栏编辑栏if (input != null)
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {eclipse怎么安装使用
e.printStackTrace();
}
}
/**
* 从输⼊流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = ad(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
ByteArray();
}
以上这篇java通过url下载⽂件并输出的⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论