struts1+mongodb download file method

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());

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326850260&siteId=291194637