一.文件导入
- 前台页面
<a class="easyui-linkbutton" data-options="iconCls:'icon-undo'"
onclick="importCsv()">导入</a>
function exportCsv() {
window.location = ctx+"/system/exportCsv";
}
- controller代码
/**
* 导出
*/
@RequestMapping("exportCsv")
@ResponseBody
public void exportCsv(HttpServletRequest request, HttpServletResponse response) throws Exception
{
List<String> dataList = codeDictManagementService.exportCsv();
response.setCharacterEncoding("UTF-8");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date time = new Date();
String tStamp = format.format(time);
String filename = "Export" + tStamp + ".csv";
response.setHeader("contentType", "text/html; charset=UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
String cp = request.getSession().getServletContext().getRealPath("/");
String path = cp + "download/" + filename;
File file = new File(path);
BufferedInputStream bis = null;
BufferedOutputStream out = null;
FileWriterWithEncoding fwwe = new FileWriterWithEncoding(file, "UTF-8");
BufferedWriter bw = new BufferedWriter(fwwe);
if (dataList != null && !dataList.isEmpty())
{
for(String data : dataList)
{
bw.write(data);
bw.write("\n");
}
}
bw.close();
fwwe.close();
try
{
bis = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
while(true)
{
int bytesRead;
if (-1 == (bytesRead = bis.read(buff, 0, buff.length)))
{
break;
}
out.write(buff, 0, bytesRead);
}
file.deleteOnExit();
}
catch(IOException e)
{
throw e;
}
finally
{
try
{
if (bis != null)
{
bis.close();
}
if (out != null)
{
out.flush();
out.close();
}
}
catch(IOException e)
{