When uploading a file, get the binary stream by reading the file and save it directly to mongodb
When downloading, first query the binary stream and then directly input it into the interface and download it
MongoManager mm = new MongoManager(); mm.getInstance(); DataProcessDao dataProcessDao = new DataProcessDao (); dataProcessDao.setDb(mm.getDB()); Initialize mongodb via if ("Y".equals(rwFxglAttachment.getIsImage())) { // Directly output to the interface to display the picture dataProcessDao.queryByName(id, response.getOutputStream()); } else { response.setContentType("application/octet-stream"); String fileName = rwFxglAttachment.getFilename() + "." + rwFxglAttachment.getExtension(); //filename must be in ios format or it will be garbled response.setHeader("Content-Disposition", "attachment; filename=" + fileName); dataProcessDao.queryByName(id, response.getOutputStream()); }
MongoManager and DataProcessDao are mongodb wrapper classes written by themselves
Mongo without wrapper class
Mongo mongo = new Mongo();
GridFS fs = new GridFS(mongo.getDb(dbName));
DBObject query = new BasicDBObject();
Pattern p = Pattern.compile(patternStr);
query.put(" filename", p);
GridFSDBFile gfsdbfile = fs.findOne(query);
return gfsdbfile.writeTo(ops);
ops wrap the class with response.getOutputStream() as above dataProcessDao.queryByName(id, response.getOutputStream());