日期:2014-05-19  浏览次数:20756 次

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 downnLoad
		String 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();
		// 清空response
		response.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> implements
		CSVReportOneService {

	@Override
	public 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 path
		String fileName = "e://Log/" + dateFm.format(date1) + "-"
				+ user_account + CSVName + "-" + "月流量报表1--测试.c