求struts图片上传和显示
如题,如何用struts向服务器上传本地图片的程序。再显示出来。如同qq空间和普通的论坛那样。还有就是用户注册时选择头像如何从数据库读取并显示。如果用户自定义头像如何使用...
如题,如何用struts向服务器上传本地图片的程序。再显示出来。如同qq空间和普通的论坛那样。还有就是用户注册时选择头像如何从数据库读取并显示。如果用户自定义头像如何使用用户上传图像。恳请附上代码和思路!鄙人仅此以200分作为感谢。496043561。
展开
展开全部
文件好几个,放在这看不清,要看的话放在email里
sturts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="upLoadForm" type="com.sec.form.UploadForm">
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="upLoadForm"
input="/index.jsp"
name="upLoadForm"
path="/upLoad"
scope="request"
type="com.sec.action.UpLoadAction" />
</action-mappings>
<message-resources parameter="com.sec.ApplicationResources" />
</struts-config>
--------------------------------------------
UploadAction.java
package com.sec.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import com.sec.form.UploadForm;
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uForm = (UploadForm)form;
FormFile file = uForm.getImg();
//保存文件
String path = getServlet().getServletContext().getRealPath("/upload");
String filePath = path + "/" + file.getFileName();
saveFile(file,filePath);
request.setAttribute("filePath",filePath);
return new ActionForward("/pages/result.jsp");
}
//文件上传
private String saveFile(FormFile file,String filePath){
String saveResult = "";
try{
//在这里把文件名(filePath)保存到数据中
//在以后显示图片时,只从数据库中读取文件名
// saveFileToDB();
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(filePath);
int readSize = 0;
byte buffer[] = new byte[1024];
while((readSize = is.read(buffer, 0, 1024)) != -1){
os.write(buffer, 0, readSize);
}
}catch(Exception e){
System.out.println("保存文件异常:"+e);
}
return saveResult;
}
}
--------------------------------------------------/index.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
<form action="/sec/upLoad.do" method="post" name="upLoadForm" enctype="multipart/form-data">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="account">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
上传图片:
</td>
<td>
<input type="file" name="img">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
<td>
<input type="button" value="清空">
</td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------
/pages/result.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String imgFile = (String)request.getAttribute("filePath");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<body>
成功以下上传图片<br>
<img src="<%= imgFile %>" width="100" height="100">
</body>
</html>
-----------------------------------------------
UploadForm.java
package com.sec.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String account;
private String password;
private FormFile img;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public FormFile getImg() {
return img;
}
public void setImg(FormFile img) {
this.img = img;
}
}
sturts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="upLoadForm" type="com.sec.form.UploadForm">
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="upLoadForm"
input="/index.jsp"
name="upLoadForm"
path="/upLoad"
scope="request"
type="com.sec.action.UpLoadAction" />
</action-mappings>
<message-resources parameter="com.sec.ApplicationResources" />
</struts-config>
--------------------------------------------
UploadAction.java
package com.sec.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import com.sec.form.UploadForm;
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uForm = (UploadForm)form;
FormFile file = uForm.getImg();
//保存文件
String path = getServlet().getServletContext().getRealPath("/upload");
String filePath = path + "/" + file.getFileName();
saveFile(file,filePath);
request.setAttribute("filePath",filePath);
return new ActionForward("/pages/result.jsp");
}
//文件上传
private String saveFile(FormFile file,String filePath){
String saveResult = "";
try{
//在这里把文件名(filePath)保存到数据中
//在以后显示图片时,只从数据库中读取文件名
// saveFileToDB();
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(filePath);
int readSize = 0;
byte buffer[] = new byte[1024];
while((readSize = is.read(buffer, 0, 1024)) != -1){
os.write(buffer, 0, readSize);
}
}catch(Exception e){
System.out.println("保存文件异常:"+e);
}
return saveResult;
}
}
--------------------------------------------------/index.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
<form action="/sec/upLoad.do" method="post" name="upLoadForm" enctype="multipart/form-data">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="account">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
上传图片:
</td>
<td>
<input type="file" name="img">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
<td>
<input type="button" value="清空">
</td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------
/pages/result.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String imgFile = (String)request.getAttribute("filePath");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<body>
成功以下上传图片<br>
<img src="<%= imgFile %>" width="100" height="100">
</body>
</html>
-----------------------------------------------
UploadForm.java
package com.sec.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String account;
private String password;
private FormFile img;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public FormFile getImg() {
return img;
}
public void setImg(FormFile img) {
this.img = img;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询