㈠ 如何在浏览器里浏览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="浏览"/>