`
xitong
  • 浏览: 6157071 次
文章分类
社区版块
存档分类
最新评论

FileAction 类是 oschina 后台用于文件库的管理,包括上传、编辑、删除和下载功能

 
阅读更多

[代码]FileAction.java




package
net.oschina.action;
002
003 importjava.io.*;
004
005 importorg.apache.commons.io.FilenameUtils;
006 importorg.apache.commons.io.IOUtils;
007 importorg.apache.commons.lang.StringUtils;
008
009 importnet.oschina.beans.File;
010 importnet.oschina.beans.User;
011 importnet.oschina.service.StorageService;
012 importnet.oschina.toolbox.LinkTool;
013 importmy.mvc.Annotation;
014 importmy.mvc.RequestContext;
015 importmy.util.Multimedia;
016
017 /**
018 * 文件库
019 * @author Winter Lau
020 * @date 2010-7-4 下午01:35:12
021 */
022 publicclassFileAction {
023
024 publicfinalstaticlongMAX_FILE_SIZE =2*1024*1024;
025
026 /**
027 * 文件下载
028 * @param ctx
029 * @throws IOException
030 */
031 publicvoiddownload(RequestContext ctx)throwsIOException {
032 if(ctx.isRobot()){
033 ctx.forbidden();
034 return;
035 }
036
037 longid = ctx.id();
038 File bean = File.INSTANCE.Get(id);
039 if(bean ==null){
040 ctx.not_found();
041 return;
042 }
043
044 String f_ident = ctx.param("fn","");
045 if(id>=100&& !StringUtils.equals(f_ident, bean.getIdent())){
046 ctx.not_found();
047 return;
048 }
049
050 // 检查下载权限
051 if(bean.IsLoginRequired() && ctx.user()==null){
052 StringBuilder login =newStringBuilder(LinkTool.home("home/login?goto_page="));
053 login.append(LinkTool.encode_url(LinkTool.action("file/download?id="+bean.getId()+"&fn="+bean.getIdent())));
054 ctx.redirect(login.toString());
055 return;
056 }
057
058 FileInputStream fis =null;
059 try{
060 java.io.File file = StorageService.FILES.readFile(bean.getPath());
061 fis =newFileInputStream(file);
062 //设置 content-type
063 ctx.response().setContentLength((int)file.length());
064 String ext = FilenameUtils.getExtension(bean.getPath());
065 String mine_type = Multimedia.mime_types.get(ext);
066 if(mine_type !=null)
067 ctx.response().setContentType(mine_type);
068 String ua = ctx.header("user-agent");
069 if(ua !=null&& ua.indexOf("Firefox")>=0)
070 ctx.header("Content-Disposition","attachment; filename*=\"utf8''"+
071 LinkTool.encode_url(bean.getName())+"."+ext+"\"");
072 else
073 ctx.header("Content-Disposition","attachment; filename="+
074 LinkTool.encode_url(bean.getName()+"."+ext));
075 IOUtils.copy(fis, ctx.response().getOutputStream());
076 bean.IncDownloadCount(1);
077 //}catch(FileNotFoundException e){
078 // ctx.not_found();
079 }finally{
080 IOUtils.closeQuietly(fis);
081 }
082 }
083
084 /**
085 * 文件上传
086 * @param ctx
087 * @throws IOException
088 */
089 @Annotation.UserRoleRequired(role=User.ROLE_EDITOR)
090 publicvoidupload(RequestContext ctx)throwsIOException {
091 File form = ctx.form(File.class);
092 if(StringUtils.isBlank(form.getName()))
093 throwctx.error("file_name_empty");
094 java.io.File file = ctx.file("file");
095 if(file ==null)
096 throwctx.error("file_empty");
097 if(!File.IsLegalFile(file.getName()))
098 throwctx.error("file_illegal");
099 String the_path = form.getUrl();
100 //判断文件是否存在
101 if(StringUtils.isNotBlank(the_path) && StorageService.FILES.exist(the_path))
102 throwctx.error("file_exists", the_path);
103 String uri = StringUtils.isBlank(the_path)?
104 StorageService.FILES.save(file):StorageService.FILES.save(file,the_path);//文件存储
105 form.setSize(file.length());
106 form.setUrl(uri);
107 form.setPath(uri);
108 form.setUser(ctx.user().getId());
109 form.setDl_count(0);
110 form.Save();
111 throwctx.error("file_upload_ok",
112 LinkTool.action("file/download?id="+form.getId()+"&fn="+form.getIdent()));
113 }
114
115 /**
116 * 文件修改
117 * @param ctx
118 * @throws IOException
119 */
120 @Annotation.UserRoleRequired(role=User.ROLE_EDITOR)
121 publicvoidedit(RequestContext ctx)throwsIOException {
122 File form = ctx.form(File.class);
123 if(StringUtils.isBlank(form.getName()))
124 throwctx.error("file_name_empty");
125 File bean = File.INSTANCE.Get(form.getId());
126 java.io.File pic = ctx.file("file");
127 if(pic !=null){
128 if(!File.IsLegalFile(pic.getName()))
129 throwctx.error("file_illegal");
130 bean.setSize(pic.length());
131 StorageService.FILES.save(pic, bean.getPath());//文件存储
132 }
133 bean.setName(form.getName());
134 bean.setDetail(form.getDetail());
135 bean.setOptions(form.getOptions());
136 bean.Update();
137 throwctx.error("file_edit_ok");
138 }
139
140 /**
141 * 文件删除
142 * @param ctx
143 * @throws IOException
144 */
145 @Annotation.UserRoleRequired(role=User.ROLE_ADMIN)
146 publicvoiddelete(RequestContext ctx)throwsIOException {
147 longid = ctx.id();
148 File file = File.INSTANCE.Get(id);
149 if(file !=null){
150 StorageService.FILES.delete(file.getPath());
151 file.Delete();
152 }
153 }
154
155 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics