Insert test data:
package com.haiyoung.biz; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * Created by Haiyoung on 2018/4/1. */ @Service public class MongoTest { @Autowired private MongoTemplate mongoTemplate; /** * add test data */ public void addData(){ List<Document> list = new ArrayList<>(); int count = 0; for(int i=0;i<10000;i++){ Document doc = new Document(); doc.append("key","key"+i); list.add(doc); count++; if(count % 300 == 0){//Insert data every 300 bars mongoTemplate.insert(list, "collection_test"); list.clear(); } } if(!list.isEmpty()){ mongoTemplate.insert(list, "collection_test"); } } }
import com.haiyoung.biz.MongoTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class DatabaseDownloadApplicationTests { @Autowired private MongoTest mongoTest; @Test public void insertData(){ mongoTest.addData(); } }
Download service class:
package com.haiyoung.biz.db_download; import com.google.gson.Gson; import com.mongodb.DBObject; import com.mongodb.MongoException; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.mongodb.core.DocumentCallbackHandler; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class MongoDownloadService { @Autowired private MongoTemplate mongoTemplate; public interface MongoCollectionPrinter{ void print(List<Object> returnList); void close(); } /** * How to export and download data * * @param collectionName The name of the collection to export the data from * @param batchNum batch write * @param printer implement download */ public void export(String collectionName, int batchNum, MongoCollectionPrinter printer){ List<Object> returnList = new ArrayList<>(); Query query = new Query(); mongoTemplate.executeQuery(query, collectionName, new DocumentCallbackHandler() { @Override public void processDocument(Document document) throws MongoException, DataAccessException { returnList.add(document); if(returnList.size() % batchNum == 0){ printer.print(returnList); returnList.clear(); } } }); if(!returnList.isEmpty()){ printer.print(returnList); returnList.clear(); } } }
Service implementation and web mapping
package com.haiyoung.biz.db_download; import com.google.gson.Gson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; @Controller @RequestMapping(value = "/page") public class MongoDownloadController { @Autowired private MongoDownloadService mongoDownloadService; private static String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"}; /** * Determine the browser type * * @param request * @return */ private static boolean isMSBrowser(HttpServletRequest request){ String userAgent = request.getHeader("User-Agent"); for(String signal : IEBrowserSignals){ if(userAgent.contains(signal)){ return true; } } return false; } /** * Implement the MongoCollectionPrinter interface * Override print and close methods */ public static class DownloadPrinter implements MongoDownloadService.MongoCollectionPrinter { private OutputStream output = null; private DownloadPrinter(HttpServletResponse response, String fileName) { response.reset();// Clear the status code and headers in the response response.setContentType("application/x-msdownload");//Set the file type response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");// set the file name try { output = response.getOutputStream();// Get the output stream } catch (IOException e) { throw new RuntimeException(e); } } @Override public void print(List<Object> returnList) { try { output.write(new Gson().toJson(returnList).getBytes("UTF-8")); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void close() { try { output.flush(); output.close(); } catch (IOException e) { throw new RuntimeException(e); }finally { if (output !=null){ try { output.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } } @RequestMapping(value = "/download") public void export(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "collectionName") String collectionName){ try { String fileName = collectionName+".txt"; if(isMSBrowser(request)){ fileName = URLEncoder.encode(fileName, "UTF-8"); }else{ fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); } DownloadPrinter printer = new DownloadPrinter(response, fileName); mongoDownloadService.export(collectionName, 300, printer); printer.close(); }catch (Exception e){ throw new RuntimeException(e); } } }
Source code link: https://github.com/Haiyoung/HyProject/tree/master/database-download