㈠ 如何在瀏覽器里瀏覽word,excel文件想通過java web輸出文件到IE瀏覽器上。看網上設置head的不行。
那個下載對話框不是有個始終詢問嗎,把這個勾去掉,點打開試試。
還有就是在文件夾選項里,找該文件類型,.doc 和 .xls ,在高級里取消下載後確認打開,勾選同一窗口瀏覽。
㈡ JavaWeb下載文件,怎麼獲取文件下載完畢的狀態
在Javaweb中,上傳下載是經常用到的功能,對於文件上傳,瀏覽器在上傳的過程中是以流的過程將文件傳給伺服器,一般都是使用commons-fileupload這個包實現上傳功能,因為commons-fileupload依賴於commons-io這個包,所以需要下載這兩個包commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar。
1、搭建環境
創建Web項目,將包導入到項目lib下
3、實現文件下載
(第一種文件下載)
注意:該代碼是直接訪問Servlet類的
?
04142package com.load;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//直接使用Http://localhost:8080/Test1/download進行下載,但是這個有缺陷,如果下載文件名中有中文,就會變成亂碼現象!@WebServlet("/download")public class download extends HttpServlet {private static final long serialVersionUID = 1L;public download() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/plain;charset=utf-8");response.setCharacterEncoding("utf-8");response.setHeader("Location","中文.txt");response.setHeader("Content-Disposition", "attachment; filename=" + "賬號.txt");OutputStream outputStream = response.getOutputStream();InputStream inputStream = new FileInputStream("E:/loads"+"/賬號.txt");byte[] buffer = new byte[1024];int i = -1;while ((i = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, i);}outputStream.flush();outputStream.close();}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
(第二種下載方法)
新建jsp頁面選擇下載
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>下載</title></head><body><a href="../DoDownloadServlet?filename=呵呵.txt">點擊下載</a></body></html>
創建Servlet類進行下載(注意:該下載如果文件名是中文的話,一樣會出現亂碼現象)
package com.load;
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;import org.hsqldb.lib.StringUtil;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;@WebServlet("/DoDownloadServlet")public class DoDownloadServlet extends HttpServlet {private static final long serialVersionUID = 1L;public DoDownloadServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//得到下載文件的名稱//String filename = request.getParameter("filename");//String filename = new String(FileName.getBytes("iso8859-1"),"UTF-8");//新建SmartUpload對象SmartUpload su = new SmartUpload();PageContext pagecontext = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);//上傳初始化su.initialize(pagecontext);//設置禁止打開該文件su.setContentDisposition(null);//下載文件try {su.downloadFile("/listener/"+filename);} catch (SmartUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
(第三種下載的方法)
同上的jsp頁面代碼,這里就不再重復了。
新建Serlvet類,實現下載功能(注意:這里文件名就算是中文名,也不會出現亂碼問題了!)
package com.load;
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;import org.hsqldb.lib.StringUtil;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;@WebServlet("/DoDownloadServlet")public class DoDownloadServlet extends HttpServlet {private static final long serialVersionUID = 1L;public DoDownloadServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//獲得文件名稱String path1 = request.getParameter("filename");//獲得路徑名稱String path = request.getSession().getServletContext().getRealPath("/listener/"+path1);// path是根據日誌路徑和文件名拼接出來的File file = new File(path);String filename = file.getName();try {//判斷是否是IE11Boolean flag= request.getHeader("User-Agent").indexOf("like Gecko")>0;//IE11 User-Agent字元串:Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko//IE6~IE10
版本的User-Agent字元串:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.0; Trident/6.0)if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") >0||flag){filename = URLEncoder.encode(filename, "UTF-8");//IE瀏覽器}else {//先去掉文件名稱中的空格,然後轉換編碼格式為utf-8,保證不出現亂碼,//這個文件名稱用於瀏覽器的下載框中自動顯示的文件名filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");//firefox瀏覽器//firefox瀏覽器User-Agent字元串://Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0} InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer;buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.addHeader("Content-Disposition", "attachment;filename=" +filename);response.addHeader("Content-Length", "" + file.length());OutputStream os = response.getOutputStream();response.setContentType("application/octet-stream");os.write(buffer);// 輸出文件os.flush();os.close();} catch (IOException e) {e.printStackTrace();}System.out.println(filename);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
㈢ java文件下載不顯示中文文件名,
//下載
response.setContentType("application/x-msdownload");
String filename = "測試.zip";
String iso_filename = SysParameter.parseGBK(filename);
response.setHeader("Content-Disposition",
"attachment;filename=" + iso_filename);
ServletOutputStream op = response.getOutputStream();
op.write(ab);
op.flush();
op.close();
SysParameter.parseGBK 方法:
// 將GBK字元轉化為ISO碼
public static String parseGBK(String sIn) {
if (sIn == null || sIn.equals(""))
return sIn;
try {
return new String(sIn.getBytes("GBK"), "ISO-8859-1");
} catch (UnsupportedEncodingException usex) {
return sIn;
}
}
㈣ javaweb jsp頁面上點擊鏈接下載文件 IE瀏覽器彈出「已經阻止從站點下載文件到您的計算機」
因為區域網有防火牆保護,所以會阻止你下載,如果你能將你的網址設置為合法網址,則IE不會阻擋。
㈤ java WEB,項目部署到伺服器,IE瀏覽器頁面中文有亂碼
本地復運行沒問題,部署到制伺服器上就有問題。那麼如果你沒有許可權修改伺服器的tomcat的service.xml文件就只能修改自己的程序了。
比如說你要傳一個test = "你好"到頁面去你可以先
test = URLEncoder.encode(test, "utf-8");這么處理一下
然後到頁面取到test之後再處理一下
test = URLDecoder.decode(test, "UTF-8");
㈥ java做web開發,如何設置一個按鈕選擇文件下載的本地路徑,求大神指導
這個簡單<input type="file" value="瀏覽"/>