struts2.0+hibernate文件上传例子。

本文介绍了一个基于Java的文件上传表单及后端处理逻辑,包括如何配置表单以支持文件上传、如何在服务器端接收并保存上传的文件、生成缩略图的过程以及与数据库交互的具体实现。

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

[size=medium]1、上传表单[/size]
<FORM id="form1" method="post" name="form1" action="newscenter/message/addImageMessage.action"  [color=red]enctype="multipart/form-data">[/color]

<DIV id="Panel1">
<DIV class="name">上传照片</DIV>
<DIV class="select"><LABEL><INPUT id="upload" type="file"
name="upload" /></LABEL></DIV></DIV>
</form>


[size=medium]2、action的写法[/size]

/**
* 添加留言,我的博览会留言模块。
*
* 实现ServletContextAware接口是因为文件上传要用到里面的方法。
*/

public class LeaveMessageAction extends ActionSupport implements
ServletContextAware{

/**
* 上传文件的表单中的name属性,当在把文件传过来的时候表单的form要添加一个属性,enctype="multipart/form-data"
*/
private File upload;// 实际上传文件
/**
* uploadContentType和uploadFileName的upload和Flie的名称要相同,类型和名称会自动填充到uploadContentType和uploadFileName中。
*/
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名

/**
* 用来获取当前程序所在的目录。
*/
private ServletContext context;


public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

/**
* 添加我的博览会的留言包含了图片上传、以及生成缩略图。
* @return
* @throws Exception
*/
public String addImageMessage() throws Exception
{

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd_HH-mm-ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String currentTime = formatter.format(curDate);


String targetFileName;
try {
//给一个虚拟路径来获取当前的一个应用程序的真实路径。
String targetDirectory = context.getRealPath("/upload/myfairImages");
//重命名上传文件的名称。
targetFileName=currentTime+"-b.jpg";
//将上传的文件名和要上传的目录保存到一个文件对象中。

File target = new File(targetDirectory, targetFileName);
//将上传文件源文件位置移动到目标文件位置,名字重命名为目标设置的文件名。要到了上传组建common.io.fileUtils中的方法。
FileUtils.copyFile(upload, target);
//再设置文件保存的位置。
setUploadFileName(target.getPath());//保存文件的存放路径


//将图片生成缩略图保存到同一个目录。
NewScaleImage thumb=new NewScaleImage(upload);
String smallImage=currentTime+"-s.jpg";
//设置生成缩略图的位置(路径+文件名字)。
thumb.setDestFile(targetDirectory+"/"+currentTime+"-s.jpg");
//设置源图像的位置(路径+文件名字)。
thumb.setSrcFileName(uploadFileName);
//按照等比(按照高度来)例来缩放图片,
thumb.resizeByHeight(120);

this.setBigImageName(targetFileName);
this.setSmallImageName(smallImage);


} catch (Exception e) {

addActionError(e.getMessage());
return "error";
}
LeaveMessageDao dao=new LeaveMessageDao();
LeaveMessage obj=new LeaveMessage();
obj.setType(type);
obj.setProvince(province);
obj.setPosition(position);
obj.setCompany(company);
obj.setName(name);
obj.setEmail(email);
obj.setContent(content);
obj.setAddTime(currentTime);
obj.setBigImageName(bigImageName);
obj.setSmallImageName(smallImageName);

if(dao.addMessage(obj))
return SUCCESS;
else
return "error";
}
}


[size=medium]3、dao中的函数[/size]

	/**
* 添加用户的留言。
* @param obj
* @return
*/
public boolean addMessage(LeaveMessage obj) {
Session session = HibernateSessionFactory.getSession();
try {

session.beginTransaction();
session.save(obj);
session.beginTransaction().commit();
System.out.println("--------------success------------");
return true;
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("------------failed----------------");
return false;
}
finally{
if(session.isOpen()){
session.close();
}
}

}



[size=medium]4生成缩略图的类在附件中。[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值