struts2上传文件

本文介绍了一个基于Struts2框架实现的新闻管理系统,涵盖了新闻的分页展示、分类筛选及上传图片等功能。系统通过HQL查询实现了新闻的动态加载,并使用了文件上传工具进行图片管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

file对象
在类写出对应的fileFileName则为该图片的名称,
fileFileUploadType则为该图片上传的类型
package cn.struts.admin;

import com.opensymphony.xwork2.ActionSupport;
import cn.jxsme.hibernate.service.articleService;
import cn.jxsme.hibernate.service.categoryService;
import cn.jxsme.hibernate.model.Article;
import cn.jxsme.hibernate.model.Category;
import cn.jxsme.springsideDao.Page;
import cn.jxsme.hj.util.PageMessage;
import cn.jxsme.hj.util.FileUtilsMyExit;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
/*
* autho huangjin green eat
*Jan 4, 2009
*/
@SuppressWarnings("serial")
public class newsManage extends ActionSupport{

private articleService articleService;//新闻业务类

private categoryService categoryService;//新闻父类业务类

private File file;//上传文件对象

private String fileFileName;//上传文件名

public List<Article> list;//新闻类list

public List<Category> listCategory;//新闻分类结果

public String pageLast;//分页信息

public String page;//当前页

public String categoryId;//当前分类的id


/**
* 一些get 和 set 方法
* @return
*/


public articleService getArticleService() {
return articleService;
}

public void setArticleService(articleService articleService) {
this.articleService = articleService;
}


public String shownews(){
// 判断当前页面输入的合法性
if(!(cn.jxsme.hj.util.IntToString.checkId(this.getPage()))){
this.setPage("1");
}
String hql ="from Article";
if(!("".equals(this.getCategoryId())|| null ==this.getCategoryId())){
hql = "from Article where category="+this.getCategoryId();
}
// String hql ="from Article where category=1";
Page page = articleService.getArticleListByPage(hql, Integer.parseInt(this.getPage()), 20L,null);
list = (java.util.List<Article>)page.getResult();
this.setPageLast(PageMessage.page("newsManage.action?", page.getTotalPageCount(), page.getTotalCount(), page.getCurrentPageNo()));
return SUCCESS;
}
/**
* 新闻添加链接
* @return
*/
public String newsAddShow(){
listCategory = categoryService.getAllCategory();
return "newsAddShow";
}
/**
* 用户新闻添加
* @return
*/
public String newsAdd(){
listCategory = categoryService.getAllCategory();
if(null != this.getFile()){
String fileName=cn.jxsme.hj.util.fileUploadUtil.getFileName(this.getFileFileName());
String filePath=cn.jxsme.hj.util.fileUploadUtil.createFilePath(this.getRealpath("/uploads/"));
System.out.println(filePath);
this.uploadImage(file, filePath, fileName);
this.setFile(null);
}
return "newsAdd";
}
/**
* 图片上传类
* @param file 上传的文件
* @param fileUp 上传的图径
* @param imgName 图片的名称
*/
public void uploadImage(File file,String fileUp,String imgName){
try {
FileUtils.copyFile(file, new File(fileUp,imgName));
} catch (IOException e) {
throw new cn.jxsme.exception.BaseException("上传文件出错:"+e);
}
}

public List<Article> getList() {
return list;
}

public void setList(List<Article> list) {
this.list = list;
}

public String getPageLast() {
return pageLast;
}

public void setPageLast(String pageLast) {
this.pageLast = pageLast;
}

public String getCategoryId() {
return categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public String getPage() {
return page;
}

public void setPage(String page) {
this.page = page;
}

@Override
public void validate() {
// TODO Auto-generated method stub
//判断当前输入的id 是否合法
if(cn.jxsme.hj.util.IntToString.checkIdSafe(this.getCategoryId())){
throw new cn.jxsme.exception.BaseException("输入不合法");
}
super.validate();
}

public categoryService getCategoryService() {
return categoryService;
}

public void setCategoryService(categoryService categoryService) {
this.categoryService = categoryService;
}

public List<Category> getListCategory() {
return listCategory;
}

public void setListCategory(List<Category> listCategory) {
this.listCategory = listCategory;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}



public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

/**
* 取得网站相对路径的物理路径
* @param path 相对路径
* @return
*/
public String getRealpath(String path){
return ServletActionContext.getServletContext().getRealPath(path);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值