java下载doc(java下载失败)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

今天给各位分享java下载doc的知识,其中也会对java下载失败进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

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

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

使用java代码下载word文件

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

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;is.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!=nullff.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("" + 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();

}

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下载doc和java下载失败的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发布于 2023-04-11 04:04:59
收藏
分享
海报
36
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了