html一个按钮下载后台服务器的word文档。java实现,但是程序没错误,但是没有提示下载 5
代码如下,求问题解决方式:@SuppressWarnings("rawtypes")@RequestMapping(value="/downloadWord")@Resp...
代码如下,求问题解决方式:
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/downloadWord")
@ResponseBody
public void downloadWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = null;// 名称
String realpath = "F:/Tomcat 6.0/word/1111.doc";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
fileName = realpath.substring(realpath.lastIndexOf("/") + 1, realpath.length());
//System.out.println(realpath);
try {
response.setContentType(this.getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="
+ fileName);
fis = new FileInputStream(realpath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
bytesRead=bis.read();
//while ((bytesRead = bis.read(buffer)) != -1) {
while (bytesRead != -1) {
//bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
bos.write(bytesRead);// 将文件发送到客户端
bytesRead = bis.read();
}
bos.close();
bis.close();
fos.close();
fis.close();
} catch (IOException e) {
response.reset();
e.printStackTrace();
} //下代码部分省略 展开
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/downloadWord")
@ResponseBody
public void downloadWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = null;// 名称
String realpath = "F:/Tomcat 6.0/word/1111.doc";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
fileName = realpath.substring(realpath.lastIndexOf("/") + 1, realpath.length());
//System.out.println(realpath);
try {
response.setContentType(this.getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="
+ fileName);
fis = new FileInputStream(realpath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
bytesRead=bis.read();
//while ((bytesRead = bis.read(buffer)) != -1) {
while (bytesRead != -1) {
//bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
bos.write(bytesRead);// 将文件发送到客户端
bytesRead = bis.read();
}
bos.close();
bis.close();
fos.close();
fis.close();
} catch (IOException e) {
response.reset();
e.printStackTrace();
} //下代码部分省略 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询