導航:首頁 > 編程語言 > 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干什麼用的 瀏覽:357
拽姐是哪個app 瀏覽:858
雲伺服器刪除了還有嗎 瀏覽:232
macbook可以用單片機嘛 瀏覽:307
南陽php招聘 瀏覽:814
去哪裡找按摩師很漂亮的app 瀏覽:818
86x99用簡便演算法計算 瀏覽:830
php截圖flash 瀏覽:273
卸載聯想app哪個好 瀏覽:720
php文字轉圖片 瀏覽:331
豆客後台怎麼加密碼 瀏覽:574
jpg轉換pdf破解版 瀏覽:978
php基礎書籍推薦 瀏覽:778
伺服器與外網不通如何驗證 瀏覽:352
電子版是不是就是文件夾 瀏覽:51
游戲屬性文件加密 瀏覽:464
如何讓安卓手機桌面圖標下移 瀏覽:530
ubuntuphp5環境搭建 瀏覽:101
賭癮解壓視頻 瀏覽:919
晉城移動dns伺服器地址 瀏覽:296