JAVA WEB用servlet下载文件不能弹出对话框
Quote: 引用:lz用下面的代码试试,以下代码再电脑上的各种浏览器都是可以用的, 而且下载输出的文件名可以是中文在android手机浏览器使用,貌似有点问题/** * 下载 * @param filePath 文件路径(物理路径) * @param fileName 输出的文件名 */public static void downLoad(String filePath, String fileName) {downLoad(new File(filePath), fileName);}/** * 下载 * @param file 文件 * @param fileName 输出的文件名 */public static void downLoad(File file, String fileName) {try {if (file.exists() && file.isFile()) {downLoad(new FileInputStream(file), fileName, file.length());}} catch (Exception e) {log.error("下载", e);throw new MvcException(e.getMessage());}}/** * 下载 * @param input 输入流 * @param fileName 输出的文件名 * @param fileSize 文件大小 */public static void downLoad(InputStream input, String fileName, long fileSize) {try {HttpServletResponse response = HttpThread.getResponse();// 线程池中取出responseString agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase();String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1);response.setHeader("Content-disposition", "attachment;filename=" + name);response.setContentType("application/octet-stream");response.setHeader("Content-Length", "" + fileSize);OutputStream output = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = input.read(buffer)) != -1) {output.write(buffer, 0, i);}input.close();output.flush();output.close();} catch (Exception e) {log.error("下载", e);}}这个。。。貌似还是不行,是不是前端有什么细节没注意到?
lz用下面的代码试试,以下代码再电脑上的各种浏览器都是可以用的, 而且下载输出的文件名可以是中文在android手机浏览器使用,貌似有点问题/** * 下载 * @param filePath 文件路径(物理路径) * @param fileName 输出的文件名 */public static void downLoad(String filePath, String fileName) {downLoad(new File(filePath), fileName);}/** * 下载 * @param file 文件 * @param fileName 输出的文件名 */public static void downLoad(File file, String fileName) {try {if (file.exists() && file.isFile()) {downLoad(new FileInputStream(file), fileName, file.length());}} catch (Exception e) {log.error("下载", e);throw new MvcException(e.getMessage());}}/** * 下载 * @param input 输入流 * @param fileName 输出的文件名 * @param fileSize 文件大小 */public static void downLoad(InputStream input, String fileName, long fileSize) {try {HttpServletResponse response = HttpThread.getResponse();// 线程池中取出responseString agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase();String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1);response.setHeader("Content-disposition", "attachment;filename=" + name);response.setContentType("application/octet-stream");response.setHeader("Content-Length", "" + fileSize);OutputStream output = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = input.read(buffer)) != -1) {output.write(buffer, 0, i);}input.close();output.flush();output.close();} catch (Exception e) {log.error("下载", e);}}
/** * 下载 * @param filePath 文件路径(物理路径) * @param fileName 输出的文件名 */public static void downLoad(String filePath, String fileName) {downLoad(new File(filePath), fileName);}/** * 下载 * @param file 文件 * @param fileName 输出的文件名 */public static void downLoad(File file, String fileName) {try {if (file.exists() && file.isFile()) {downLoad(new FileInputStream(file), fileName, file.length());}} catch (Exception e) {log.error("下载", e);throw new MvcException(e.getMessage());}}/** * 下载 * @param input 输入流 * @param fileName 输出的文件名 * @param fileSize 文件大小 */public static void downLoad(InputStream input, String fileName, long fileSize) {try {HttpServletResponse response = HttpThread.getResponse();// 线程池中取出responseString agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase();String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1);response.setHeader("Content-disposition", "attachment;filename=" + name);response.setContentType("application/octet-stream");response.setHeader("Content-Length", "" + fileSize);OutputStream output = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = input.read(buffer)) != -1) {output.write(buffer, 0, i);}input.close();output.flush();output.close();} catch (Exception e) {log.error("下载", e);}}
* @param fileName 输出的文件名 * @param fileSize 文件大小 */public static void downLoad(InputStream input, String fileName, long fileSize) {try {HttpServletResponse response = HttpThread.getResponse();// 线程池中取出responseString agent = HttpThread.getRequest().getHeader("USER-AGENT").toLowerCase();String name = new String(fileName.getBytes(agent.indexOf("msie") != -1 ? "GBK" : CharEncoding.UTF_8), CharEncoding.ISO_8859_1);response.setHeader("Content-disposition", "attachment;filename=" + name);response.setContentType("application/octet-stream");response.setHeader("Content-Length", "" + fileSize);OutputStream output = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = input.read(buffer)) != -1) {output.write(buffer, 0, i);}input.close();output.flush();output.close();} catch (Exception e) {log.error("下载", e);}}