一、使用文件流的形式在浏览器下载word文档
String path = "http://172.16.228.130:7300/document/d9dd1ec7e72d4fc6a9e50ef1a930f34d.docx";
String fileName = "测试文档";
BufferedOutputStream bf = null;
String fileName = fileName;
try {
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
bf = new BufferedOutputStream(response.getOutputStream());
if (path.startsWith("http://")) {
bf.write(this.httpConverBytes(path);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bf != null) {
bf.close();
bf.flush();
}
}
public byte[] httpConverBytes(String path) {
BufferedInputStream in = null;
ByteArrayOutputStream out = null;
URLConnection conn = null;
try {
URL url = new URL(path);
conn = url.openConnection();
in = new BufferedInputStream(conn.getInputStream());
out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
byte[] content = out.toByteArray();
return content;
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}