mongdb_GridFs的事例使用

1.GridFS 的文件同样是保存在 db.collection 中,通常使用 fs.files 存储文件元数据信息,fs.chunks 存储文件内容。

下面介绍下文件的上传及下载使用:

jsp

<form  action="../../common/Attachment/UploadFile.action" method="post" enctype="multipart/form-data">
					<div class="span8">
						<input type="file" id="xxxx_唯一" name="importfile" maxfilesize="2147483648" />
						<input type="hidden" id="id" name="id" value="">
                                               <input type="hidden" id="worklistid" name="worklistid" value="">
                             <input type='hidden' name='importfileid' value=''
 </div>
					<div class="span4">
						<button   class="btn btn-purple btn-small">上传 <i class="icon-upload-alt"></i></button>
					</div>
				</form>

java

    private String id;
    private File importfile;
    private String importfileid;
    private String type;
@SuppressWarnings("unchecked")
	public String UploadFile() throws IOException, NoSuchDotException {
		if(importfile==null || !importfile.isFile()){
			print("{\"success\":false,\"msg\":\"文件内容过大,上传失败!\"}");
			return NONE;
		}
		
		long currentTime = System.currentTimeMillis();
		String filepath = "" + currentTime;
		String filename = this.getRequest().getParameter("xxx");
		String mongoid = "";
		try {

			FileInputStream fin = new FileInputStream(importfile);  
			mongoid = MongoDB.saveFile(importfile, currentTime + "_" + filename);
			fin.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String attachid = this.getRequest().getParameter("attachid");
		Attachment attachment = new Attachment();
		if(attachid!=null&&!"".equals(attachid)){
			attachment = attachmentService.getAttachmentById(attachid);
		}else{
			attachment.setId(0);
		}
		attachment.setFilepath(filepath);
		attachment.setName(filepath + "_" + filename);
		attachment.setType(type);
		attachment.setSavetype(Attachment.TYPE_MONGO);
		attachment.setMongoid(mongoid);
		attachment.setFatherid(Integer.valueOf(id));
		attachment.setUploadtime(currentTime/1000L);
		attachmentService.InsertOrUpdateAttachment(attachment);
		if(Integer.valueOf(id)<0){
			List<Attachment> files = (List<Attachment>) this.getSession().getAttribute("importfiles");
			if(files==null)
				files = new ArrayList<Attachment>();
			files.add(attachment);
			this.getSession().setAttribute("importfiles", files);
		}
	
		this.getResponse().getWriter().print("{\"success\":true}");*/
		printHTML("<span>{\"success\":true}</span>");
		return NONE;
	}

获取附件:

public String getAttachments() throws IOException {
		String candelete = this.getRequest().getParameter("dn");
		String param = "";
		if(this.getRequest().getParameter("param")!=null)
			param = this.getRequest().getParameter("param");
		StringBuffer json = new StringBuffer();
		if(Integer.valueOf(id)>0){
			List<Attachment> files = attachmentService.getAttachmentList(" and id="+id);
			for(Attachment attach:files){
				if("true".equals(candelete))
					json.append("<li><button class='btn btn-minier btn-success' onclick='download"+param+"("+attach.getId()+");'><i class='icon-cloud-download'> 下载</i></button>  " +
							"<button class='btn btn-minier btn-success' onclick='deleteattach"+param+"("+attach.getId()+");'><i class='icon-trash'> 删除</i></button>   " +
							"<b>"+attach.getName()+"</b></li>");
				else
					json.append("<li><button class='btn btn-minier btn-success' onclick='download"+param+"("+attach.getId()+");'><i class='icon-cloud-download'> 下载</i></button>   " +
							"<b>"+attach.getName()+"</b></li>");
			}
		}else{
			List<Attachment> files = (List<Attachment>) this.getSession().getAttribute("importfiles");
			if(files!=null){
				for(Attachment attach:files){
					if("true".equals(candelete))
						json.append("<li><button class='btn btn-minier btn-success' onclick='download"+param+"("+attach.getId()+");'><i class='icon-cloud-download'> 下载</i></button>  " +
								"<button class='btn btn-minier btn-success' onclick='deleteattach"+param+"("+attach.getId()+");'><i class='icon-trash'> 删除</i></button>   " +
								"<b>"+attach.getName()+"</b></li>");
					else
						json.append("<li><button class='btn btn-minier btn-success' onclick='download"+param+"("+attach.getId()+");'><i class='icon-cloud-download'> 下载</i></button>   " +
								"<b>"+attach.getName()+"</b></li>");
				}
			}
		}
		print("{\"success\":true,\"attachs\":\""+json.toString()+"\"}");
		return NONE;
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值