action源码 java_OSChina 的 FileAction 类源码 (FileAction.java)

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

RequestContext 类请看

这里。Annotation 请看

这里。

1.[代码]FileAction.java

package net.oschina.action;

import java.io.*;

import org.apache.commons.io.FilenameUtils;

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang.StringUtils;

import net.oschina.beans.File;

import net.oschina.beans.User;

import net.oschina.service.StorageService;

import net.oschina.toolbox.LinkTool;

import my.mvc.Annotation;

import my.mvc.RequestContext;

import my.util.Multimedia;

/**

* 文件库

* @author Winter Lau

* @date 2010-7-4 下午01:35:12

*/

public class FileAction {

public final static long MAX_FILE_SIZE = 2 * 1024 * 1024;

/**

* 文件下载

* @param ctx

* @throws IOException

*/

public void download(RequestContext ctx) throws IOException {

if(ctx.isRobot()){

ctx.forbidden();

return ;

}

long id = ctx.id();

File bean = File.INSTANCE.Get(id);

if(bean == null){

ctx.not_found();

return ;

}

String f_ident = ctx.param("fn", "");

if(id>=100 && !StringUtils.equals(f_ident, bean.getIdent())){

ctx.not_found();

return ;

}

// 检查下载权限

if(bean.IsLoginRequired() && ctx.user()==null){

StringBuilder login = new StringBuilder(LinkTool.home("home/login?goto_page="));

login.append(LinkTool.encode_url(LinkTool.action("file/download?id="+bean.getId()+"&fn="+bean.getIdent())));

ctx.redirect(login.toString());

return ;

}

FileInputStream fis = null;

try{

java.io.File file = StorageService.FILES.readFile(bean.getPath());

fis = new FileInputStream(file);

//设置 content-type

ctx.response().setContentLength((int)file.length());

String ext = FilenameUtils.getExtension(bean.getPath());

String mine_type = Multimedia.mime_types.get(ext);

if(mine_type != null)

ctx.response().setContentType(mine_type);

String ua = ctx.header("user-agent");

if(ua != null && ua.indexOf("Firefox")>=0)

ctx.header("Content-Disposition","attachment; filename*=\"utf8''" +

LinkTool.encode_url(bean.getName())+"."+ext+"\"");

else

ctx.header("Content-Disposition","attachment; filename=" +

LinkTool.encode_url(bean.getName()+"."+ext));

IOUtils.copy(fis, ctx.response().getOutputStream());

bean.IncDownloadCount(1);

//}catch(FileNotFoundException e){

//ctx.not_found();

}finally{

IOUtils.closeQuietly(fis);

}

}

/**

* 文件上传

* @param ctx

* @throws IOException

*/

@Annotation.UserRoleRequired(role=User.ROLE_EDITOR)

public void upload(RequestContext ctx) throws IOException {

File form = ctx.form(File.class);

if(StringUtils.isBlank(form.getName()))

throw ctx.error("file_name_empty");

java.io.File file = ctx.file("file");

if(file == null)

throw ctx.error("file_empty");

if(!File.IsLegalFile(file.getName()))

throw ctx.error("file_illegal");

String the_path = form.getUrl();

//判断文件是否存在

if(StringUtils.isNotBlank(the_path) && StorageService.FILES.exist(the_path))

throw ctx.error("file_exists", the_path);

String uri = StringUtils.isBlank(the_path)?

StorageService.FILES.save(file):StorageService.FILES.save(file,the_path);//文件存储

form.setSize(file.length());

form.setUrl(uri);

form.setPath(uri);

form.setUser(ctx.user().getId());

form.setDl_count(0);

form.Save();

throw ctx.error("file_upload_ok",

LinkTool.action("file/download?id="+form.getId()+"&fn="+form.getIdent()));

}

/**

* 文件修改

* @param ctx

* @throws IOException

*/

@Annotation.UserRoleRequired(role=User.ROLE_EDITOR)

public void edit(RequestContext ctx) throws IOException {

File form = ctx.form(File.class);

if(StringUtils.isBlank(form.getName()))

throw ctx.error("file_name_empty");

File bean = File.INSTANCE.Get(form.getId());

java.io.File pic = ctx.file("file");

if(pic != null){

if(!File.IsLegalFile(pic.getName()))

throw ctx.error("file_illegal");

bean.setSize(pic.length());

StorageService.FILES.save(pic, bean.getPath());//文件存储

}

bean.setName(form.getName());

bean.setDetail(form.getDetail());

bean.setOptions(form.getOptions());

bean.Update();

throw ctx.error("file_edit_ok");

}

/**

* 文件删除

* @param ctx

* @throws IOException

*/

@Annotation.UserRoleRequired(role=User.ROLE_ADMIN)

public void delete(RequestContext ctx) throws IOException {

long id = ctx.id();

File file = File.INSTANCE.Get(id);

if(file != null){

StorageService.FILES.delete(file.getPath());

file.Delete();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值