252 lines
8.4 KiB
Java
252 lines
8.4 KiB
Java
package com.base.util;
|
||
|
||
import java.io.BufferedInputStream;
|
||
import java.io.File;
|
||
import java.io.FileInputStream;
|
||
import java.io.OutputStream;
|
||
|
||
import javax.servlet.ServletException;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import javax.servlet.http.HttpSession;
|
||
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.awe.Configure;
|
||
|
||
public class DownloadAttUtil {
|
||
|
||
public static void downloadAppIOS(String sFileName, String sPath,
|
||
HttpServletResponse response) throws Exception {
|
||
// sFileName = URLEncoder.encode(sFileName, "GBK");
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
response.reset();
|
||
response.setHeader("Content-disposition", "attachment; filename="
|
||
+ sFileName);
|
||
File file = new File(sPath);
|
||
response.setContentLength((int) file.length());
|
||
FileInputStream fileInputStream = null;
|
||
BufferedInputStream bufferedInputStream = null;
|
||
OutputStream outputStream = null;
|
||
try {
|
||
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
|
||
// 读出文件到response
|
||
// 这里是先需要把要把文件内容先读到缓冲区
|
||
// 再把缓冲区的内容写到response的输出流供用户下载
|
||
fileInputStream = new FileInputStream(file);
|
||
bufferedInputStream = new BufferedInputStream(
|
||
fileInputStream);
|
||
byte[] b = new byte[bufferedInputStream.available()];
|
||
bufferedInputStream.read(b);
|
||
outputStream = response.getOutputStream();
|
||
outputStream.write(b);
|
||
} catch (Exception e) {
|
||
ARE.getLog().error(e);
|
||
throw new Exception("输入流文件出现异常 :" + e);
|
||
} finally {
|
||
// 人走带门
|
||
bufferedInputStream.close();
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
fileInputStream.close();
|
||
}
|
||
|
||
//
|
||
//
|
||
//
|
||
//
|
||
// try (InputStream in = new FileInputStream(sPath)) {
|
||
// FileUtils.copyFile(new File(sPath), response.getOutputStream());
|
||
// } catch (Exception e) {
|
||
// ARE.getLog().error(e);
|
||
// throw new Exception("输入流文件出现异常 :" + e);
|
||
// }
|
||
}
|
||
|
||
public static void downloadAppAndroid(String sFileName, String sPath,
|
||
HttpServletResponse response) throws Exception {
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
response.reset();
|
||
response.setHeader("Content-disposition", "attachment; filename="
|
||
+ sFileName);
|
||
File file = new File(sPath);
|
||
response.setContentLength((int) file.length());
|
||
FileInputStream fileInputStream = null;
|
||
BufferedInputStream bufferedInputStream = null;
|
||
OutputStream outputStream = null;
|
||
try {
|
||
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
|
||
// 读出文件到response
|
||
// 这里是先需要把要把文件内容先读到缓冲区
|
||
// 再把缓冲区的内容写到response的输出流供用户下载
|
||
fileInputStream = new FileInputStream(file);
|
||
bufferedInputStream = new BufferedInputStream(
|
||
fileInputStream);
|
||
byte[] b = new byte[bufferedInputStream.available()];
|
||
bufferedInputStream.read(b);
|
||
outputStream = response.getOutputStream();
|
||
outputStream.write(b);
|
||
} catch (Exception e) {
|
||
ARE.getLog().error(e);
|
||
throw new Exception("输入流文件出现异常 :" + e);
|
||
} finally {
|
||
// 人走带门
|
||
bufferedInputStream.close();
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
fileInputStream.close();
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 描述:下载附件设置response
|
||
*
|
||
* @param fileName
|
||
* @param path
|
||
* @param fileSize
|
||
* @param response
|
||
* @param request
|
||
* @throws Exception
|
||
*/
|
||
public static void downloadAtt(String sFileName, String sPath,
|
||
String sFileSize, HttpServletResponse response,
|
||
HttpServletRequest request, String sDevType) throws Exception {
|
||
if ("ios".equals(sDevType))
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
else
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
response.reset();
|
||
response.setHeader("Content-disposition", "attachment; filename="
|
||
+ sFileName);
|
||
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
|
||
HttpSession session;
|
||
Configure CurConfig;
|
||
session = request.getSession(true);
|
||
if ((session == null) || (session.getAttributeNames() == null)) {
|
||
ARE.getLog().error("读取配置文件错误!请检查配置文件。");
|
||
throw new Exception("------Timeout------");
|
||
}
|
||
|
||
CurConfig = Configure.getInstance(session.getServletContext());
|
||
if (CurConfig == null) {
|
||
ARE.getLog().error("读取配置文件错误!请检查配置文件。");
|
||
throw new ServletException("读取配置文件错误!请检查配置文件。");
|
||
}
|
||
String path1 = CurConfig.getConfigure("FileSavePath");
|
||
File file = new File(path1 + "/" + sPath);
|
||
if (!file.exists()) {
|
||
ARE.getLog().error("读取文件错误!请检查[文件。");
|
||
throw new ServletException("读取文件错误!请检查[文件。");
|
||
}
|
||
response.setContentLength((int) file.length());
|
||
// if (sFileSize != null && !"".equals(sFileSize)) {
|
||
// response.setContentLength(Integer.valueOf(sFileSize));
|
||
// } else {
|
||
// }
|
||
FileInputStream fileInputStream = null;
|
||
BufferedInputStream bufferedInputStream = null;
|
||
OutputStream outputStream = null;
|
||
try {
|
||
// 读出文件到response
|
||
// 这里是先需要把要把文件内容先读到缓冲区
|
||
// 再把缓冲区的内容写到response的输出流供用户下载
|
||
fileInputStream = new FileInputStream(file);
|
||
bufferedInputStream = new BufferedInputStream(
|
||
fileInputStream);
|
||
byte[] b = new byte[bufferedInputStream.available()];
|
||
bufferedInputStream.read(b);
|
||
outputStream = response.getOutputStream();
|
||
outputStream.write(b);
|
||
} catch (Exception e) {
|
||
ARE.getLog().error(e);
|
||
throw new Exception("输入流文件出现异常 :" + e);
|
||
} finally {
|
||
// 人走带门
|
||
bufferedInputStream.close();
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
fileInputStream.close();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 描述:设置response编码 编码默认UTF-8
|
||
*
|
||
* @param response
|
||
* @param sCharset
|
||
*/
|
||
public static void setCharset(HttpServletResponse response, String sCharset) {
|
||
if (null == sCharset)
|
||
sCharset = "";
|
||
if (sCharset.length() < 1)
|
||
sCharset = "UTF-8";
|
||
response.setContentType("text/html; charset=" + sCharset);
|
||
}
|
||
|
||
/**
|
||
* 描述:下载附件设置response
|
||
*
|
||
* @param fileName
|
||
* @param path
|
||
* @param fullpath
|
||
* @param fileSize
|
||
* @param response
|
||
* @param request
|
||
* @throws Exception
|
||
*/
|
||
public static void downloadAtt(String sFileName, String sPath,
|
||
String sFileSize, HttpServletResponse response,
|
||
HttpServletRequest request, String sDevType,String fullpath) throws Exception {
|
||
if ("ios".equals(sDevType))
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
else
|
||
sFileName = new String(sFileName.getBytes(), "ISO-8859-1");
|
||
response.reset();
|
||
response.setHeader("Content-disposition", "attachment; filename="
|
||
+ sFileName);
|
||
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
|
||
HttpSession session;
|
||
Configure CurConfig;
|
||
session = request.getSession(true);
|
||
if ((session == null) || (session.getAttributeNames() == null)) {
|
||
ARE.getLog().error("读取配置文件错误!请检查配置文件。");
|
||
throw new Exception("------Timeout------");
|
||
}
|
||
|
||
CurConfig = Configure.getInstance(session.getServletContext());
|
||
if (CurConfig == null) {
|
||
ARE.getLog().error("读取配置文件错误!请检查配置文件。");
|
||
throw new ServletException("读取配置文件错误!请检查配置文件。");
|
||
}
|
||
File file = new File(fullpath);
|
||
if (!file.exists()) {
|
||
ARE.getLog().error("读取文件错误!请检查[文件。");
|
||
throw new ServletException("读取文件错误!请检查[文件。");
|
||
}
|
||
response.setContentLength((int) file.length());
|
||
FileInputStream fileInputStream = null;
|
||
BufferedInputStream bufferedInputStream = null;
|
||
OutputStream outputStream = null;
|
||
try {
|
||
// 读出文件到response
|
||
// 这里是先需要把要把文件内容先读到缓冲区
|
||
// 再把缓冲区的内容写到response的输出流供用户下载
|
||
fileInputStream = new FileInputStream(file);
|
||
bufferedInputStream = new BufferedInputStream(
|
||
fileInputStream);
|
||
byte[] b = new byte[bufferedInputStream.available()];
|
||
bufferedInputStream.read(b);
|
||
outputStream = response.getOutputStream();
|
||
outputStream.write(b);
|
||
} catch (Exception e) {
|
||
ARE.getLog().error(e);
|
||
throw new Exception("输入流文件出现异常 :" + e);
|
||
} finally {
|
||
// 人走带门
|
||
bufferedInputStream.close();
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
fileInputStream.close();
|
||
}
|
||
}
|
||
} |