📄 blogphotoaction.java
字号:
package com.easyjf.blog.web.action;
import java.io.File;
import org.apache.commons.fileupload.FileItem;
import com.easyjf.blog.domain.Photo;
import com.easyjf.blog.logic.PhotoService;
import com.easyjf.blog.logic.impl.PhotoServiceImpl;
import com.easyjf.util.CommUtil;
import com.easyjf.util.FileUtil;
import com.easyjf.web.Globals;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;
import com.easyjf.web.tools.IActiveUser;
import com.easyjf.web.tools.IPageList;
public class BlogPhotoAction extends BaseCrudAction {
private PhotoService photoService = PhotoServiceImpl.getInstance();
public Object doBefore(WebForm form, Module module) {
blog=userService.getUserBlog(this.getCurrentUser(form).getUserName());
return super.doBefore(form, module);
}
public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
return photoService.queryPhoto(this.getCurrentUser(form).getUserName(),
"", currentPage, pageSize);
}
public Page doAdd(WebForm form, Module module, IActiveUser user) {
Photo obj=(Photo)form2Obj(form);
obj.setInputUser(user.getUserName());
FileItem file=(FileItem)form.get("file");
if(file!=null)
{
String clientName = file.getName();
if(!FileUtil.isImgageFile(clientName))
{
form.addResult("msg","只能上传图片文件及附件文件!");
return module.findPage("edit");
}
else
uploadFile(file,obj);
}
boolean ret=photoService.addPhoto(obj);
if(ret)
{
form.addResult("msg","添加成功!");
return super.doQuery(form,module,user);
}
else
{
form.addResult("msg","添加失败!");
return module.findPage("edit");
}
}
public Page doDel(WebForm form, Module module, IActiveUser user) {
boolean ret=photoService.delPhoto((String)form.get("cid"));
form.addResult("msg",ret?"删除成功!":"删除失败!");
return super.doQuery(form,module,user);
}
public Page doUpdate(WebForm form, Module module, IActiveUser user) {
boolean ret=photoService.updatePhoto((Photo)form.get("cid"));
form.addResult("msg",ret?"修改成功!":"修改失败!");
return super.doQuery(form,module,user);
}
public Object form2Obj(WebForm form) {
String cid = CommUtil.null2String(form.get("cid"));
Photo obj = photoService.getPhoto(cid);
if (obj == null)
obj = new Photo();
form.toPo(obj);
return obj;
}
public void uploadFile(FileItem file,Photo obj)
{
String clientName = file.getName();
FileUtil.isImgageFile(clientName);
if(file!=null)
{
String filePath = Globals.APP_BASE_DIR
+ "upfile/blog/" + blog.getCid() + "/";
File fdir = new File(filePath);
if (!fdir.exists())
fdir.mkdirs();
try{
String fileName=clientName.substring(clientName.lastIndexOf(File.separator)+1);
file.write(new File(filePath + fileName));
obj.setUrl("/upfile/blog/" + blog.getCid() +"/"+ fileName);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -