changeset 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 b27a99201cbe
children 2ac9b2e3fb57
files src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java
diffstat 1 files changed, 34 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java	Mon Jul 03 23:48:53 2017 +0200
+++ b/src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java	Mon Jul 03 23:52:13 2017 +0200
@@ -4,6 +4,7 @@
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletOutputStream;
 
 import org.apache.commons.lang.StringUtils;
 import org.json.JSONObject;
@@ -15,36 +16,48 @@
 
 public class DownloadTabDelimited4File extends AbstractServletMethod {
 	public static String name = "downloadTabDelimited4File";
-	
+
 	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
-		
+
 		Long fileId = getQueryLongParam(request, "fileId");
-		
+
 		if(fileId != null){
 			LGFile file = DataProvider.getInstance().getFile(fileId);
 			if(file != null){
-				
+
 				String text = HTTPUtils.getTabDelimitedOfFile(fileId);	// get text as tab-delimited file
-					
+
 				if(StringUtils.isNotEmpty(text)){
-				
-					String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";   
-					
+
+					String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";
+
 					//filename extension with csv
 					filename = filename.substring(0, filename.length()-3);
 					filename += "csv";
-					
-					response.setContentType("text/csv; charset=UTF-8"); 
-					response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
-				
-					
-					PrintWriter out = response.getWriter();
-					out.print(text);
-					out.flush();
-					
-				}	
-							
-				
+
+					response.setContentType("text/csv; charset=UTF-8");
+					response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
+
+                    byte[] csv = text.getBytes("UTF-8");
+
+                    response.setContentLength( csv.length + 3 );
+
+                    ServletOutputStream aOutputStream = response.getOutputStream();
+
+                    /* Specify the BOM (Byte order Mask) for UTF-8
+                     * so that Excel can identify the encoding and open it in the correct format
+                     * */
+                    aOutputStream.write(new byte[] {(byte)0xef, (byte)0xbb, (byte)0xbf});
+
+                    aOutputStream.write(csv);
+
+                    aOutputStream.flush();
+
+                    aOutputStream.close();
+
+				}
+
+
 			} else{
 				response.setContentType("application/json");
 				JSONObject json = new JSONObject();
@@ -64,5 +77,5 @@
 			out.flush();
 		}
 	}
-	
+
 }