struts1+mongodb下载文件方法
上传文件时通过读取文件得到二进制流直接保存到mongodb
下载时先查询出二进制流然后直接输入到界面并下载
?
?
MongoManager mm = new MongoManager();mm.getInstance();DataProcessDao dataProcessDao = new DataProcessDao();dataProcessDao.setDb(mm.getDB());初始化mongodb 通过if ("Y".equals(rwFxglAttachment.getIsImage())) {//直接输出到界面显示图片dataProcessDao.queryByName(id, response.getOutputStream());} else {response.setContentType("application/octet-stream");String fileName = rwFxglAttachment.getFilename() + "." + rwFxglAttachment.getExtension();//filename必须是ios格式 否则会乱码response.setHeader("Content-Disposition", "attachment; filename=" + fileName);dataProcessDao.queryByName(id, response.getOutputStream());}
?MongoManager 和 DataProcessDao 是自己写的mongodb包装类
?
不用包装类的方式
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用response.getOutputStream() 如上面包装类dataProcessDao.queryByName(id, response.getOutputStream());
?