Servlet1.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "application/msword"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\本人.doc");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.
read(); }
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}jsp1.jsp
<%@ page contentType="application/msword"%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<imag src="Servlet1" border="0">
</BODY>
</HTML>假如运行jsp1.jsp,那么就出现下载提示框,我不想要这种提示,我期望一调用就显示文档,怎么做呀?
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "application/msword"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream("c:\\本人.doc");
int b = fis.read();
while(b!=-1){
out.write(b);
b = fis.
read(); }
fis.close();
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,resp);
}
}jsp1.jsp
<%@ page contentType="application/msword"%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<imag src="Servlet1" border="0">
</BODY>
</HTML>假如运行jsp1.jsp,那么就出现下载提示框,我不想要这种提示,我期望一调用就显示文档,怎么做呀?
解决方案 »
- Hibernate中使用Creiteria,结果以List的形式返回。求打印出List中的内容
- jsp基础题
- 一直在线等,求解决!!!!!
- struts2 + spring + hibernate 整合开发中,spring的getHibernateTemplate()方法操作数据库问题
- 在接口方法前面的@WebMethod(exclude=true),这样设置是什么意思啊?
- 高手请进 div如何盖住object 急急急
- 如何在状态拦中显示IP,
- 请问怎么样用路径连接access数据库?????
- A和B谁的资源消耗大?
- 项目打jar包后,访问外部配置
- 难题:若Array[][]类型中存放file、image等对象,如何提交和接收?(不能单独发送file、image)
- 文件操作字符串替换问题!!!
e.g.<a href="1.doc">
文件头上加:<%@ page contentType="application/msword"%>
tomcat\conf\web.xml里:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
我的word文档是在数据库里取出来的。而且没有命名,只是二进制流,到时候我只想通过调用Servlet1来显示。怎么做呀?
setNameForDownload(nameForDownloadRef); HttpServletResponse response = getResponse();
try
{
byte[] buffer = new byte[fileContentRef.length()];
buffer = (fileContentRef.toString()).getBytes();
output = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename=youfilename);
output.write(buffer,0,buffer.length);
output.flush(); }
catch(IOException ex)
{
System.out.println(ex);
}
finally
{
try
{
output.close();
}
catch(IOException ioexception1)
{
}
}