comparison src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java @ 96:a025dd907626

Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Mon, 03 Jul 2017 23:52:13 +0200
parents b8ad346e39a0
children
comparison
equal deleted inserted replaced
95:b27a99201cbe 96:a025dd907626
2 2
3 import java.io.PrintWriter; 3 import java.io.PrintWriter;
4 4
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpServletResponse;
7 import javax.servlet.ServletOutputStream;
7 8
8 import org.apache.commons.lang.StringUtils; 9 import org.apache.commons.lang.StringUtils;
9 import org.json.JSONObject; 10 import org.json.JSONObject;
10 11
11 import de.mpiwg.gazetteer.bo.LGFile; 12 import de.mpiwg.gazetteer.bo.LGFile;
13 import de.mpiwg.gazetteer.utils.FileManager; 14 import de.mpiwg.gazetteer.utils.FileManager;
14 import de.mpiwg.gazetteer.utils.HTTPUtils; 15 import de.mpiwg.gazetteer.utils.HTTPUtils;
15 16
16 public class DownloadTabDelimited4File extends AbstractServletMethod { 17 public class DownloadTabDelimited4File extends AbstractServletMethod {
17 public static String name = "downloadTabDelimited4File"; 18 public static String name = "downloadTabDelimited4File";
18 19
19 public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{ 20 public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
20 21
21 Long fileId = getQueryLongParam(request, "fileId"); 22 Long fileId = getQueryLongParam(request, "fileId");
22 23
23 if(fileId != null){ 24 if(fileId != null){
24 LGFile file = DataProvider.getInstance().getFile(fileId); 25 LGFile file = DataProvider.getInstance().getFile(fileId);
25 if(file != null){ 26 if(file != null){
26 27
27 String text = HTTPUtils.getTabDelimitedOfFile(fileId); // get text as tab-delimited file 28 String text = HTTPUtils.getTabDelimitedOfFile(fileId); // get text as tab-delimited file
28 29
29 if(StringUtils.isNotEmpty(text)){ 30 if(StringUtils.isNotEmpty(text)){
30 31
31 String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt"; 32 String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";
32 33
33 //filename extension with csv 34 //filename extension with csv
34 filename = filename.substring(0, filename.length()-3); 35 filename = filename.substring(0, filename.length()-3);
35 filename += "csv"; 36 filename += "csv";
36 37
37 response.setContentType("text/csv; charset=UTF-8"); 38 response.setContentType("text/csv; charset=UTF-8");
38 response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 39 response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
39 40
40 41 byte[] csv = text.getBytes("UTF-8");
41 PrintWriter out = response.getWriter(); 42
42 out.print(text); 43 response.setContentLength( csv.length + 3 );
43 out.flush(); 44
44 45 ServletOutputStream aOutputStream = response.getOutputStream();
45 } 46
46 47 /* Specify the BOM (Byte order Mask) for UTF-8
47 48 * so that Excel can identify the encoding and open it in the correct format
49 * */
50 aOutputStream.write(new byte[] {(byte)0xef, (byte)0xbb, (byte)0xbf});
51
52 aOutputStream.write(csv);
53
54 aOutputStream.flush();
55
56 aOutputStream.close();
57
58 }
59
60
48 } else{ 61 } else{
49 response.setContentType("application/json"); 62 response.setContentType("application/json");
50 JSONObject json = new JSONObject(); 63 JSONObject json = new JSONObject();
51 json.put("status", "error"); 64 json.put("status", "error");
52 json.put("message", "File no found (" + fileId + ")"); 65 json.put("message", "File no found (" + fileId + ")");
62 PrintWriter out = response.getWriter(); 75 PrintWriter out = response.getWriter();
63 out.print(json.toString()); 76 out.print(json.toString());
64 out.flush(); 77 out.flush();
65 } 78 }
66 } 79 }
67 80
68 } 81 }