ie 可以直接打开doc文件的.你出现什么问题? 只要作个连接就可以了.
解决方案 »
- jsp checkbox() 全选以及批量删除的问题
- javamail邮件发送 移植到linux 不能发送
- javascript调用oxc控件,怎么判断oxc控件注册是否成功,非常非常重要的一个问题
- 一個jdk編譯的問題!
- 如何将jsp中生成的表格导入excel中?急用,多谢相告!!!
- Tomcat的问题
- 怎样得到数据库的所有表的名字?
- 请教resin环境下jsp访问数据库如何能读汉字
- jsp中向英文版的sql中写中文数据,出来是乱码,有解决方法吗?
- could not find action or result 困扰一上午了,求指教
- Tomcat问题
- 怎么在JAVA/JSP中调用MSSQL的自定义函数?
java.io.IOException: Not an ISO 8859_1 character:星
at org.apache.tomcat.core.BufferedServletOutputStream.print(BufferedServletOutputStream.java:221)我是这样写的
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bout = new BufferedOutputStream(out);
response.setContentType("appliction/*;charset=GB2312");
response.setHeader("Content-disposition","attachment:FileView");这是什么意思?
然后我的servlet按照提交的id号码,打开存在服务目录下的该doc文件.
这是一个测试程序,后来要改为从数据库内查询得到再显示该doc文档。
一起copy了下来。你找找吧,应该有你想要的吧?Java中对文件的操作 java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。 1。新建目录<%@ page contentType="text/html;charset=gb2312"%>
<%
String filePath="c:/aaa/";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%> 2。新建文件<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
String filePath="c:/哈哈.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent = "中文测试".toString();
myFile.println(strContent);
resultFile.close();
%> 3。删除文件<%@ page contentType="text/html;charset=gb2312"%>
<%
String filePath="c:/支出证明单.xls";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
myDelFile.delete();
%> 4。文件拷贝<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
file://读到流中
InputStream inStream=new FileInputStream("c:/aaa.doc");
FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
%> 5。整个文件夹拷贝<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%String url1="C:/aaa";
String url2="d:/java/";
(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
file[i].toString();
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+(file[i].getName()).toString());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
}
%> 6。文件下载<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "zsc104.swf".toString();
//读到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%> 7。数据库字段中的文件下载<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
int bytesum=0;
int byteread=0;
//打开数据库
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得数据库中的数据
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();file://将数据库中的数据读到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%> 8。把网页保存成文件<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
URL stdURL = null;
BufferedReader stdIn = null;
PrintWriter stdOut = null;
try {
stdURL = new URL("http://www.163.com");
}
catch (MalformedURLException e) {
throw e;
}try {
stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html")));
}
catch (IOException e) {
} /***把URL指定的页面以流的形式读出,写成指定的文件***/
try {
String strHtml = "";
while((strHtml = stdIn.readLine())!=null) {
stdOut.println(strHtml);
}
}
catch (IOException e) {
throw e;
}
finally {
try {
if(stdIn != null)
stdIn.close();
if(stdOut != null)
stdOut.close();
}
catch (Exception e) {
System.out.println(e);
}
}
%> 9。直接下载网上的文件<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
int bytesum=0;
int byteread=0;URL url = new URL("http://pimg.163.com/sms/micheal/logo.gif");
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs=new FileOutputStream( "c:/abc.gif"); byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
10。按行读文件 <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.io.*" %> <% FileReader myFileReader=new FileReader("c:/哈哈.txt"); BufferedReader myBufferedReader=new BufferedReader(myFileReader); String myString=null; String resultString=new String(); while((myString=myBufferedReader.readLine())!=null) { resultString=resultString+myString+"<br>"; } out.println(resultString); myFileReader.close(); %>
<%@ page contentType="application/msword" %>
<!-- 以上这行设定本网页为excel格式的网页 -->
<%
response.setHeader("Content-disposition","inline; filename=test1.doc"); //线上浏览方式
// response.setHeader("Content-disposition","attachment; filename=test1.doc");//下载方式
//以上这行设定传送到前端浏览器时的档名为test1.doc
//就是靠这一行,让前端浏览器以为接收到一个word档
%>
//然后输出动态内容就可以得到一个word文档了/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1,打开:
1)文件头上加:<%@ page contentType="application/msword"%>
xml文件里:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
2)可以用js,以下代码来自引用:
<HTML>
<script>
var wrd=new ActiveXObject("Word.Application")
wrd.visible=true
alert("您的"+wrd.Application.Caption+"安装路径为:\n"+wrd.Application.Path+"\n版本号是:"+wrd.Application.version+"\n注册使用者是:"+wrd.Application.UserName)
wrd.Documents.Add()
//wrd.Documents.Open("c:\\exam.doc")
wrd.Selection.TypeText("This is some text.")
wrd.Application.Activate()
wrd.ActiveDocument.SaveAs("c:\\exam111.doc")
wrd=null
</script>
</HTML>
2,下载:
<%
// 得到文件名字和路径
String filename = "MengxianhuiDocTest.doc";
String filepath = "D:\\";
// 设置响应头和下载保存的文件名
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename + "\"");
// 打开指定文件的流信息
java.io.FileInputStream fileInputStream =
new java.io.FileInputStream(filepath + filename);
// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
%>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
File file = new File("1.doc");
byte[] content = null;
int flength = (int)file.length();
content = new byte[flength];
FileInputStream bin = new FileInputStream(file);
ServletOutputStream servletOut = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(servletOut);
bos.write(content);
bos.flush() ;
bos.close() ;tomcat就是报错阿!还有人指点吗?