对于java开发来说,通常情况下会对前端页面的数据进行一个下载保存,并且存在各种存储方式,我在网上也找了许多,但是呢,大多数的一样,我也照葫芦画个碗,上代码了
/***
*
* @param path 下载路径
* @param request
* @param response
* @return true下载成功 false下载失败
*/
public static boolean PdfAndWordUploadUtil(String path,
HttpServletRequest request,
HttpServletResponse response){
/*下载状态 true完成 false为完成*/
boolean flag=false;
File file=new File(path);
String fileName=file.getName();
String ext=fileName.substring(fileName.lastIndexOf(".")+1);
String agent=request.getHeader("USER-AGENT"); //判断浏览器类型
try {
if(agent!=null && agent.indexOf("Fireforx")!=-1) {
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); //UTF-8编码,防止输出文件名乱码
} else {
fileName= URLEncoder.encode(fileName,"UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedInputStream bis=null;
OutputStream os=null;
response.reset();
response.setCharacterEncoding("utf-8");
if(ext=="docx") {
response.setContentType("application/msword"); // word格式
}else if(ext=="pdf") {
response.setContentType("application/pdf"); // word格式
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
try {
bis=new BufferedInputStream(new FileInputStream(file));
byte[] b=new byte[bis.available()+1000];
int i=0;
os = response.getOutputStream(); //直接下载导出
while((i=bis.read(b))!=-1) {
os.write(b, 0, i);
}
os.flush();
os.close();
flag=true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(os!=null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
这是一个我自己封装的工具类,也有写注解,可以大概的看明白了
这里在写一个生产Excel的方法,也是自己封装的一个类,可以看一下哟
package cn.com.taiji.system.vo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.List;
/**
* excle下载
*/
public class ExcelDown {
/***
*
* @param list 总数据对象集合
* @param title 标题
* @param obj1 表头数据
* @param Scheent 工作簿名
* @param obj2 每一列的数据对象
* @param request
* @param response
*/
public static Boolean exportExcle(List<?> list,
String title,
String[]obj1,
String Scheent,
List<String[]>obj2,
HttpServletRequest request, HttpServletResponse response){
boolean state=true;
XSSFWorkbook book = new XSSFWorkbook();
String fileName = title;
if (list != null && list.size() > 0) {
XSSFSheet sheet = book.createSheet(Scheent);
String[] vals = obj1;
createExcel(sheet, 0, vals);
for (int i = 0; i < list.size(); i++) {
String []vals2=obj2.get(i);
createExcel(sheet, i + 1, vals2);
}
response.setContentType("text/html;charset=utf-8");
BufferedOutputStream bos = null;
String repName = fileName.endsWith(".xls")?fileName:fileName+".xls";
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
try {
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
repName = URLEncoder.encode(repName, "UTF-8");
} else {
repName = new String(repName.getBytes("UTF-8"), "ISO8859-1");
}
response.setHeader( "Content-Disposition", "attachment;filename=\"" + repName + "\"");
bos = new BufferedOutputStream(response.getOutputStream());
book.write(bos);
} catch (IOException e) {
state=false;
e.printStackTrace();
} finally{
if(bos != null){
try{
bos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
return state;
}
private static void createExcel(XSSFSheet sheet, int rowNo, String[] vals) {
if (vals.length > 0) {
XSSFRow row = sheet.createRow(rowNo);
for (int i = 0; i < vals.length; i++) {
String val = vals[i];
XSSFCell cell = row.createCell(i);
cell.setCellValue(val);
}
}
}
}