标签:filename erro buffere pes trace irf buffered mes enter
/**
* 大纲附件1
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
@SuppressWarnings("rawtypes")
public ActionForward dgfjxzKczy(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
Frmuser user = (Frmuser) request.getSession().getAttribute(GlobalNames.USER_SESSION_KEY);
log.debug(user.getUseraccount() + ":enter dgfjxzKczy");
String type = request.getParameter("type");
String [] kcids = request.getParameter("kcids").split(",");
OutputStream outputLoction = null;
OutputStream toClient = null;
InputStream fis = null;
try {
//打印zip
//先创建一个临时文件夹,用于存打印的excel用,打包完成后再删除
System.gc();
Random random = new Random();
int num1 = random.nextInt(1000);
int num2 = random.nextInt(1000);
String randDicName = (num1*num2)+"_"+"kcdgfj";
String randDicPath = request.getSession().getServletContext().getRealPath("") +File.separator+"template"+File.separator+randDicName;
this.creatNewFimeDic(randDicName, randDicPath);
for(int k=0;k<kcids.length;k++){
List<Map> mapList=Context.getDao().execSqlQueryToMap("select file_path,filename from jx02_jxdg where jx02id=‘"+kcids[k]+"‘");
if(StringUtils.nullOrBlank(mapList)){
continue;
}
String path = mapList.get(0).get("file_path")==null?"":mapList.get(0).get("file_path").toString();
if(StringUtils.nullOrBlank(path)){
continue;
}
String filePath = request.getRealPath("/");
filePath = path.replace("/", File.separator);
String fileName = mapList.get(0).get("filename").toString();
File file = new File(filePath);
if(!file.exists()){
continue;
}
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
//目录下存在的文件
fis = new BufferedInputStream(new FileInputStream(filePath));
//输出到压缩包的文件
OutputStream os=new FileOutputStream(randDicPath+File.separator+fileName);
//toClient = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024 * 1024 * 8];
int i = -1;
while ((i = fis.read(buffer)) != -1) {
// toClient.write(buffer, 0, i);
// toClient.flush();
os.write(buffer, 0, i);
}
os.flush();
os.close();
}
for(int k=0;k<kcids.length;k++){
List<Map> mapList=Context.getDao().execSqlQueryToMap("select file_path_two file_path,filename_two filename from jx02_jxdg where jx02id=‘"+kcids[k]+"‘");
if(StringUtils.nullOrBlank(mapList)){
continue;
}
String path = mapList.get(0).get("file_path")==null?"":mapList.get(0).get("file_path").toString();
if(StringUtils.nullOrBlank(path)){
continue;
}
String filePath = request.getRealPath("/");
filePath = path.replace("/", File.separator);
String fileName = mapList.get(0).get("filename").toString();
File file = new File(filePath);
if(!file.exists()){
continue;
}
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
fis = new BufferedInputStream(new FileInputStream(filePath));
OutputStream os=new FileOutputStream(randDicPath+File.separator+fileName);
//toClient = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024 * 1024 * 8];
int i = -1;
while ((i = fis.read(buffer)) != -1) {
// toClient.write(buffer, 0, i);
// toClient.flush();
os.write(buffer, 0, i);
}
os.flush();
os.close();
}
// outputLoction = new FileOutputStream(randDicPath+"/"+""+".xls");
OutputStream output = null;
//开始打包
String zipName = "大纲附件";
boolean flag = this.doPackgeFile(randDicPath, zipName);
if(flag){
//下载
try {
// 下载已经压缩好的文件
String filePath_Zip = randDicPath+"/"+zipName+".zip";
output = response.getOutputStream();
response.reset();
response.setContentType("application/x-msdownload");
String filename = new String(zipName.getBytes("GBK"),"iso8859-1");
response.addHeader("Content-Disposition", "attachment;filename="+filename+".zip");
FileInputStream input = new FileInputStream(filePath_Zip);
byte[] b = new byte[1024*10];
int i = 0;
while ((i = input.read(b)) > 0) {
output.write(b, 0, i);
}
output.flush();
input.close();
output.close();
System.gc();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
this.delFolder(randDicPath);
}
//删除临时文件
this.delFolder(randDicPath);
}
}catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(),e);
if(outputLoction != null){
try {
outputLoction.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}finally{
//关闭流
if(toClient!= null ){
try {
toClient.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(fis!= null ){
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(outputLoction != null){
try {
outputLoction.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return null;
}
//创建临时文件夹
private boolean creatNewFimeDic(String dicName,String path){
try {
File dirfile = new File(path);
if (!dirfile.exists()&&!dirfile.isDirectory()) {
dirfile.mkdirs();
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
标签:filename erro buffere pes trace irf buffered mes enter
原文地址:https://www.cnblogs.com/worklife/p/13176532.html