Struts2.0 图片的上传与下载的实现

时间:2022-05-24 03:59:38

图片上传的JSP页面,即register.jsp。此处要注意:要上传图片,必须要在form中加入此代码,enctype="multipart/form-data",否则,上传无效的。

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Register</title>

</head>
<body>
<s:form action="Register.action" method="post" theme="simple"
enctype="multipart/form-data">
<table align="left" width="50%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td align="right"><s:text name="user.username"></s:text>:</td>
<td align="left" height="30" valign="middle"><s:textfield
name="username" key="user.username"></s:textfield></td>
</tr>
<tr>
<td align="right"><s:text name="user.password"></s:text>:</td>
<td align="left" height="30" valign="middle"><s:textfield
name="password" key="user.password"></s:textfield></td>
</tr>
<tr>
<td align="right" height="30" valign="middle"><s:text
name="user.register.sex"></s:text>:</td>
<td align="left"><s:radio name="sex"
list="%{#{'1':'男','0':'女'}}" value="1" theme="simple"></s:radio></td>
</tr>
<tr>
<td align="right" height="30" valign="middle"><s:text
name="user.register.upload"></s:text>:</td>
<td align="left"><s:file label="上传" theme="simple"
name="upload" /></td>
</tr>
<tr>
<td align="right"><s:submit class="btn" value="完成"></s:submit></td>
<td align="center"><s:reset class="btn" value="重置"></s:reset></td>
</tr>
</table>
</s:form>

</body>
</html>


RegisterAction.java中,通过get、set方法来获取表单传来的值即可。此处注意upload类型为File即可。

package com.struts.action;

import java.io.File;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.struts.bean.UserBean;
import com.struts.dao.UserInfoDAO;

public class RegisterAction extends ActionSupport {

private String username="";
private String password="user";
private int sex;
private File upload;





public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

public File getUpload() {
return upload;
}

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

public String execute() throws Exception {
if(username.equalsIgnoreCase("")){
return "input";
}

UserInfoDAO userDAO=new UserInfoDAO();
int result=userDAO.registerUser(username,password,sex,upload);
if (result != 0){
return "success";
}

else{
return "error";
}


}

}

UserInfoDAO.java中的registerUser()代码实现。数据库中upload的字段类型为:mediumblob。所以,此处用setBlob。

public int registerUser(String username, String password, int sex,
File upload) throws SQLException, IOException {
//FileInputStream in = null;
conn = ConnectDB.getConn();
//System.out.println(upload);
//in = new FileInputStream(upload);
String strSql = "insert into userlist(username,sex,upload,password)values(?,?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setString(1, username);
pstmt.setInt(2, sex);
//pstmt.setBinaryStream(3, in, in.available());
pstmt.setBlob(3, new FileInputStream(upload));
pstmt.setString(4, password);
result = pstmt.executeUpdate();
return result;

}


以上为图片的上传。很简单吧。

下面即为图片的下载了。

首先,我们要实现一个人员在查看详细信息时,要求显示这个人的头像。即:userdetails.jsp页面。

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@page import="com.struts.bean.UserBean"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
UserBean userBean = (UserBean) request.getAttribute("userBean");
String username = userBean.getUsername();
int sex = userBean.getSex();
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Details</title>

</head>
<body>
<s:form action="UserInfo.action" method="post" theme="simple">
<table align="left" width="30%" border="1" cellpadding="0"
cellspacing="0">
<tr>
<td align="right"><s:text name="user.username"></s:text>:</td>
<td align="left" height="30" valign="middle"><%=username%></td>
</tr>
<tr>
<td align="right"><s:text name="user.register.sex"></s:text>:</td>
<td align="left" height="30" valign="middle">
<%
if (sex == 0) {
%><s:text name="女"></s:text> <%
} else {
%> <s:text name="男"></s:text> <%
}
%>
</td>
</tr>

<tr>
<td align="right"><s:text name="user.register.upload"></s:text>:</td>
<td><img name="upload"
src="GetPhoto.action?username=<%=username%>" width="128"
height="128" /></td>
</tr>
<tr>
<td align="right"><s:submit class="btn" value="返回"></s:submit></td>
</tr>

</table>
</s:form>

</body>
</html>

以上的userBean,即把,人员的详细信息封装在UserBean中。通过在后台中的UserDetailsAction.java中,以set、get方法来传到前台来。如下:

package com.struts.action;

import com.struts.bean.UserBean;
import com.struts.dao.UserInfoDAO;

public class UserDetailsAction {
private String username;
private UserBean userBean;
private int sex;
public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

public UserBean getUserBean() {
return userBean;
}

public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String execute() throws Exception {
UserInfoDAO userDetails = new UserInfoDAO();
UserBean userBean = userDetails.queryUserDetails(username);
this.userBean = userBean;
return "userdetails";

}
}

具体后台的queryUserDetails()方法的实现就不贴出来了,很简单的了。

现在来看userdetails.jsp页面中的<img name="upload"
     src="GetPhoto.action?username=<%=username%>" width="128"
     height="128" />

以下即是GetPhotoAction.java的实现。

package com.struts.action;

import java.io.InputStream;


import com.opensymphony.xwork2.ActionSupport;
import com.struts.dao.UserInfoDAO;

public class GetPhotoAction extends ActionSupport{
private String username;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception {

return "success";
}
public InputStream getPhoto() throws Exception{
UserInfoDAO userDetails = new UserInfoDAO();
return userDetails.getPhoto(username);
}

}



上段代码中getPhoto()方法的实现。此段方法放在UserInfoDAO.java中。

public InputStream getPhoto(String username) throws SQLException,
IOException {

conn = ConnectDB.getConn();
String sql = "select upload from userlist where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();

if (rs.next()) {
Blob bo = rs.getBlob("upload");
BufferedInputStream ins = new BufferedInputStream(
bo.getBinaryStream());
int count = (int) bo.length();
byte[] bt = new byte[count];
ins.read(bt, 0, count);
return new ByteArrayInputStream(bt);

}
return null;

}


struts.xml中的代码,如下:

<action name="GetPhoto" class="com.struts.action.GetPhotoAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">photo</param>
<param name="bufferSize">1024</param>
</result>
</action>


struts.xml中的<param name="inputName">photo</param>   此处的photo要与GetPhotoAction.java中getPhoto()方法中相对应。

以上即为简单的实现,还有很多不足的地方。

例如只能上传jpg格式的图片,下载的时候图片的名称也不能为中文(此处需要用到过滤器来实现即可)。

 之所以写这些博客,主要是供自己以后学习的哈。

不过,也能帮助到其他人的吧。我在实现之前,一直有找资料。

发现网上有好多都是一样的,同样一段实现代码被copy来copy去的。

汗,也许是自己笨吧,别人的代码不太适合自己现在的这个框架,所以,自己多花了点时间,实现了一个哈。

如有不足之处,还望大家多多指教。共同进步哈。O(∩_∩)O~