导航:首页 > 编程语言 > java下载doc

java下载doc

发布时间:2022-08-27 15:37:42

java文件下载如何实现

在http协议下,实现下载一般就两种方法,一个采用cont-type="";此种方法为附件的方式下载;;
另一种较简单,就是你只需要点下载按钮然后跳转到服务器的那个文件路劲就可以了,浏览器自动回进行下载..

❷ 求大神解决用JAVA实现下载后,下载.doc文件内容乱码问题!在线等!

需要看你的流是用什么包装的,比如用BufferedReader或者是BufferedWriter是不可以的

❸ jsp中怎么利用java需要将在oracle数据库中存在的pdf,doc等文件下载下来。最好有代码

首先你要明确一个概念,数据库中是不可能存这些文件的,存的最多是这些文件对应的地址,是String类型的数据。
在这基础上来看这些代码。注意标注的1234:
//获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载
String path = servletContext.getRealPath("/");

//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)
response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");
ServletOutputStream out;
//通过文件路径获得File对象(假如此路径中有一个download.pdf文件)
File file = new File(path + "download/" + "download.pdf");

try {
FileInputStream inputStream = new FileInputStream(file);

//3.通过response获取ServletOutputStream对象(out)
out = response.getOutputStream();

int b = 0;
byte[] buffer = new byte[512];
while (b != -1){
b = inputStream.read(buffer);
//4.写到输出流(out)中
out.write(buffer,0,b);
}
inputStream.close();
out.close();
out.flush();

} catch (IOException e) {
e.printStackTrace();
}

❹ 如何在官网下载Java的API文档

题主你好,

首先导航到jdk下载的首页:

*.如果题主有图形界面的话, 直接双击api步录下的index.html就可以.

-----

希望可以帮到题主, 欢迎追问.

❺ java下载doc/docx文件乱码的问题

java下载文件是在什么平台(win,linux?)运行的,又是如何打开看到乱码的(手动、自动、何种方式)?

❻ 使用java代码下载word文件

我同事在做项目的时候也遇到这个问题,应该是插件本身生成的docx文件是xml格式的,有些版本的word打开是会有提示,如果没有必要最好生成doc格式的word

❼ 哪里可以下载离线版的java doc

官网下载。

http://www.oracle.com/technetwork/java/javase/documentation/index.html

http://docs.oracle.com/javase/7/docs/api/

❽ java 代码实现下载.doc文件

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*"%>
<%!
public String toUtf8String(String s)
{
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
%>
<%
String filename=new String(request.getParameter("filename").getBytes("ISO8859-1"),"GBK");
String dirName="D:/我.doc";
java.io.File ff=null;
String dd=dirName+System.getProperties().getProperty("file.separator")+filename;
try{
ff=new java.io.File(dd);
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
if (ff!=null&&ff.exists()&&ff.isFile())
{
long filelength = ff.length();
InputStream inStream=new FileInputStream(dd);
//设置输出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.setContentLength((int)filelength);
response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
out.clear();
out = pageContext.pushBody();
}
%>

:<a href="d.jsp">aa</a>

上面的那个是用流写的 但是也可以用超链接下载
你写上文件的路径就可以了

❾ Java 下载文件的方法怎么写

参考下面
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}

// 下载本地文件
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
String fileName = "Operator.doc".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 下载网络文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

//支持在线打开文件的一种方式
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}

阅读全文

与java下载doc相关的资料

热点内容
卸载联想app哪个好 浏览:719
php文字转图片 浏览:328
豆客后台怎么加密码 浏览:574
jpg转换pdf破解版 浏览:978
php基础书籍推荐 浏览:775
服务器与外网不通如何验证 浏览:351
电子版是不是就是文件夹 浏览:50
游戏属性文件加密 浏览:462
如何让安卓手机桌面图标下移 浏览:528
ubuntuphp5环境搭建 浏览:99
赌瘾解压视频 浏览:917
晋城移动dns服务器地址 浏览:294
php开源文库系统 浏览:134
android记事本源码 浏览:406
安卓11小游戏怎么玩法 浏览:188
gif有损压缩 浏览:936
windows下安装linux命令操作 浏览:842
米家app怎么设置进门亮灯 浏览:652
任我行服务器为什么会影响截图 浏览:296
安卓留言板怎么删除 浏览:18