J2EE下载服务器端文件
CSV其实就是COMMA SEPARATED VALUE的缩写。
在开发中用Java操作csv文件有专门的的API叫javacsv.jar
javacsv.jar下载地址:
http://sourceforge.net/project/showfiles.php?group_id=33066
?
down.jsp
??
<!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=ISO-8859-1"><title>Insert title here</title></head><body><input type="button" value="点击即可下载" onclick="work();" /><hr />浏览文件:<input type="file" name="path" id="path"></body><SCRIPT LANGUAGE="JavaScript">var uname = "<sec:authentication property='name'></sec:authentication>";function work() {//important!window.location = "user/reqDownload?account=" + uname;}</SCRIPT></html>?
?
?
UserController类:
?
@RequestMapping(value = "/reqDownload", method = RequestMethod.GET)public void reqDownload(@RequestParam("account") String user_account,HttpServletResponse response, HttpServletRequest request,ModelMap map) throws IOException {request.setCharacterEncoding("UTF-8");// get the filePath that will be downnLoadString path = CSVReportOneService.ReqCSVFile(user_account,"reqFileName");// 获取下载文件路径// String path=request.getParameter("reqFileName");// 编码// path=URLEncoder.encode(path, "ISO-8859-1");// 解码// path=URLDecoder.decode(path, "UTF-8");File file = new File(path);// 文件名String fileName = file.getName();// 扩展名//String ext = fileName.substring(fileName.lastIndexOf(".") + 1,//fileName.length()).toUpperCase();// 输入流InputStream inStream = new FileInputStream(path);InputStream in = new BufferedInputStream(inStream);byte[] bs = new byte[in.available()];in.read(bs);in.close();// 清空responseresponse.reset();// 设置response的Header// 使浏览器弹出下载对话框response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "ISO-8859-1"));response.addHeader("Content-Length", "" + file.length());// 输出流OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(bs);toClient.flush();toClient.close();// map.addAttribute("xml", path);}?
CSVReportOneServiceImpl类方法
??
import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import com.rg.report.entity.CSVTest;import com.rg.report.service.CSVReportOneService;import com.rg.report.utils.CSV.Java2CSV;public class CSVReportOneServiceImpl extends BaseServiceImpl<CSVTest> implementsCSVReportOneService {@Overridepublic String ReqCSVFile(String user_account, String CSVName) {// 从获取将要写入csv文件的结果集String sql = "SELECT user_id,user_account ,user_name,user_desc FROM rg_users";List<CSVTest> list = findByNativeQuery(sql.toString(), CSVTest.class,null);// 预组装csv文件内容标题行String[][] data = new String[list.size() + 1][4];data[0][0] = "user_id";data[0][1] = "user_account";data[0][2] = "user_name";data[0][3] = "user_desc";// 预组装csv文件内容int len = list.size();for (int i = 0; i < len; i++) {data[i + 1][0] = list.get(i).getUser_id();data[i + 1][1] = list.get(i).getUser_account();data[i + 1][2] = list.get(i).getUser_name();data[i + 1][3] = list.get(i).getUser_desc();}Date date1 = new Date();SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd-HHmmss");// file store pathString fileName = "e://Log/" + dateFm.format(date1) + "-"+ user_account + CSVName + "-" + "月流量报表1--测试.csv";//write CSV.file on localhostJava2CSV.writerCsv(fileName, data);return fileName;}}?
?CSVTest.java
??
import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class CSVTest {@IdString user_id;String user_account;String user_name;String user_desc;public String getUser_id() {return user_id;}public void setUser_id(String user_id) {this.user_id = user_id;}public String getUser_account() {return user_account;}public void setUser_account(String user_account) {this.user_account = user_account;}public String getUser_name() {return user_name;}public void setUser_name(String user_name) {this.user_name = user_name;}public String getUser_desc() {return user_desc;}public void setUser_desc(String user_desc) {this.user_desc = user_desc;}}?
?
?
?
?