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;
}