[size=medium]1、上传表单[/size]
[size=medium]2、action的写法[/size]
[size=medium]3、dao中的函数[/size]
[size=medium]4生成缩略图的类在附件中。[/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]