您的位置:首页 > 编程语言 > Java开发

struts2上传照片到服务器端并以上传时间命名照片文件

2017-02-11 13:51 423 查看
要做的是struts2中在前端表单中上传照片到服务器(TOMCAT),这里是上传一个学生信息的照片,学生表的字段为

Student(id,name,sex,phone,grade,photo)这里的photo字段是string类型,存放的是存储照片的相对路径。

1.前端页面的一些处理:

(1)这里的表单必须设置属性 method="post"  enctype="multipart/form-data" ,可以看到根据struts.xml配置文件应该将表单提交到action中

(2)在上传照片的标签处要设置一个隐藏变量,以Domain Model的方式传入后端Action中这个photo值。这是为了在修改照片之前,显示的还是上一次上传的照片(在页面上做了一个显示照片的模块,通过这个上传过去的相对路径能显示照片)

(3)这里是一个修改页面的代码片段,前面在注册页面,将一些基本信息存入数据库的时候,在后端将学生信息存放到session对象的一个属性中。

表单代码:

<form method="post" enctype="multipart/form-data" action="student/updatestudent">
<table class="bordered">
<tr><td width="285">用户名:</td><td width="320"><input type="text" name="student.name" value="${sessionScope.STUDENT.name}"/>
<input type="hidden" name="student.id" value="${sessionScope.STUDENT.id }"/>
</td></tr>
<tr><td>旧密码:</td><td><input type="password" id="oldpwd"/></td></tr>
<tr><td>新密码:</td><td><input type="password" name="student.pwd" value="${sessionScope.STUDENT.pwd}"/></td></tr>
<tr><td>重复新密码:</td><td><input type="password" id="student.pwd1" value="${sessionScope.STUDENT.pwd}"/></td></tr>
<tr>
<td>性别:</td>
<td><s:radio list="#{'男':'男','女':'女'}" name="student.sex" value="#session.STUDENT.sex" theme="simple"/></td></tr>
<tr><td>年级:</td><td><s:select list="#{'大一':'大一','大二':'大二','大三':'大三','大四':'大四'}" name="student.grade" value="#session.STUDENT.grade" theme="simple"/></td>
</tr>
<tr><td>联系电话:</td><td><input type="text" name="student.phone" value="${sessionScope.STUDENT.phone}"/></td></tr>
!这里是上传照片的标签
<tr><td height="23">上传头像:</td><td><input type="file" name="image" />
<input type="hidden" name="student.photo" value="${sessionScope.STUDENT.photo}"/>
</td></tr>
<tr><td height="23" colspan="2"><input type="submit" value="提交"></td></tr>
</table>
</form>


2.明确Action中方法要做的事情,对应编写DAO中的方法

(1)上传照片的话,应该要注意:

a.设置数据库表中的字段值photo,相对路径(确认照片命名方式,然后设置字段值)。

b.上传到服务器端,这里是绝对路径下将照片文件复制到服务器端的目录下。

c.用户可能是修改照片,之前上传的照片应该在服务器端删除,避免出现很多的垃圾文件。这里就要在DAO中写一个得到相对路径的方法。

d.在DAO中编写一个方法,更新学生除照片以为通过domain model方式提交上来的其他信息。

(2)后端Action:

a.struts2上传照片,要在Action中定义三个属性和每个属性对应的getter和setter。

//上传照片文件需要定义的三个变量属性和其setter getter
private File image;//对应前端表单中提交 name="image"
private String imageContentType;
private String imageFileName;

public File getImage() {
return image;
}

public void setImage(File image) {
this.image = image;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String getImageContentType() {
return imageContentType;
}

public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
b.在Action中的方法中以上传时间为名字命名照片文件,如:uploadfiles/2015/03/15122018005.jpg。并加以判断 当image不为空时再进行 上传到服务器、设置数据库表的相对路径属性、删除之前上传的照片  这些事情。
//学生用户修改自己的信息后保存更新到数据库,上传照片文件保存到服务器端
public String updatestudent()
{
//获得request对象
HttpServletRequest request=ServletActionContext.getRequest();
//获得application对象
ServletContext application=request.getServletContext();
//获得session对象
HttpSession session=request.getSession();
Calendar cal=Calendar.getInstance();//获得一个 截取时间的 实例

//要判断用户是否上传了照片,也就是image属性是否为空,不为空时再做服务器端存储照片的操作
if(image!=null)
{

//想要保存照片文件的绝对路径形式:uploadfiles/2015/03/15122018005.jpg,是以上传时间
//命名照片文件

//如果保存上传文件的根目录不存在,创建根目录
String fileName="uploadfiles";
//为了设置photo属性,定义一个变量url获取相对路径
String url=fileName;
fileName=application.getRealPath("")+fileName;//前边加上服务器端路径
File file=new File(fileName);
if(!file.exists())
{
//创建根目录
file.mkdirs();
}

//如果年的目录不存在,创建年的目录
int year= cal.get(Calendar.YEAR);//获得年
fileName=fileName+"\\"+year;
url=url+"\\"+year;
file=new File(fileName);
if(!file.exists())
{
//创建年目录
file.mkdirs();
}

//如果月的目录不存在,创建月份的目录
int month=cal.get(Calendar.MONTH)+1;//这里获得月记住要加1
fileName=fileName+"\\"; //这是最后一次加反斜杠,后边不创建目录
url=url+"\\";
//这里月份小于10的时候设置(例如5月)成 05
if(month<10)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+month;
url=url+month;
file=new File(fileName);
if(!file.exists())
{
//创建月目录
file.mkdirs();
}

//从这里开始就是照片文件名字中的命名,不涉及到创建目录,生成文件名的日部分
int day=cal.get(Calendar.DAY_OF_MONTH);//这里获得月记住要加1
fileName=fileName+"\\";
url=url+"\\";
//这里日数小于10的时候设置(例如5日)成 05
if(day<10)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+day;
url=url+day;

//生成文件名的小时部分
int hour=cal.get(Calendar.HOUR_OF_DAY);
//如果小时数是个位,就加0
if(hour<10)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+hour;
url=url+hour;

//生成文件名的分钟部分
int mintue=cal.get(Calendar.MINUTE);
if(mintue<10)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+mintue;
url=url+mintue;

//生成文件名的秒部分
int second=cal.get(Calendar.SECOND);
if(second<10)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+second;
url=url+second;

//生成文件名的毫秒部分
int millisecond=cal.get(Calendar.MILLISECOND);
//毫秒应该加两个零
if(millisecond<10)
{
fileName=fileName+"0";
url=url+"0";
}
if(millisecond<100)
{
fileName=fileName+"0";
url=url+"0";
}
fileName=fileName+millisecond;
url=url+millisecond;

//生成文件名的拓展名部分

String extension=imageFileName.substring(imageFileName.indexOf("."));//获取上传文件的拓展名
fileName=fileName+extension;
url=url+extension;
System.out.println(fileName);
File myfile=new File(fileName);
try {
FileUtils.copyFile(image,myfile);//上传到客户端
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//删除之前上传到服务器端的照片
String oldurl= sdao.getUrlById(student);
if(oldurl!=null)
{
String oldFileName=application.getRealPath("")+oldurl;
File oldFile=new File(oldFileName);
if(oldFile.exists())
{
FileUtils.deleteQuietly(oldFile);
}
}
//设置数据库中student表中photo字段,设置为相对路径
student.setPhoto(url);
}
//因为之前保存到数据库中学生信息存放到了session中,为了修改成功后跳回主页面再点修改的时候显示更新后的信息,也要更新一下session
session.setAttribute("STUDENT",student);
//调用DAO中方法,将表单中更新的数据保存到数据库中
sdao.updateStudent(student);
return "main";//根据配置文件跳转到main.jsp页面

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息