在struts2框架上上传多张图片

本文介绍了一个使用JSP实现的用户信息上传功能,并通过Action类处理上传的数据,最终在succ.jsp页面展示用户信息及上传的照片。

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

1.index.jsp首页

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   
	 <form action="file2" method="Post" enctype="multipart/form-data">
    	姓名:<input name="name" /></br>
    	年龄:<input name="age" /></br>
    	性别:<input name="sex" /></br>
    	照片1:<input name="photo" type="file" /></br>
    	照片2:<input name="photo" type="file" /></br>
    	照片3:<input name="photo" type="file" /></br>
    	<input name="submit" type="submit" /></br>
    </form>
  </body>
</html>

2.action对应的类

package cn.acion;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class FileAction {

	private String name;
	private Integer age;
	private String sex;

	// 设置成数组,存放多个图片
	private File[] photo;
	private String[] photoFileName;
	private String savePath;
	private String[] imgSrc;
	
	public String execute(){
		
		ServletContext context = ServletActionContext.getServletContext();
		
		this.imgSrc = new String[this.photo.length];
		//获取路径
		String realSavePath = context.getRealPath(savePath);
		File saveDirectory = new File(realSavePath);
		if( !saveDirectory.exists() )
			saveDirectory.mkdir();
		//用循环对图片处理
		for( int i = 0;i<this.photo.length;i++ ){
			String photoFileName = this.photoFileName[i];
			String saveFileName = UUID.randomUUID().toString();
			String ext = photoFileName.substring(photoFileName.lastIndexOf("."));
			
			String outFilePath = realSavePath + "/" + saveFileName + ext;
			
			this.imgSrc[i] = this.savePath + "/" + saveFileName + ext;
			
			this.imgSrc[i] = imgSrc[i].substring(1);
			
			
			try {
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(photo[i]));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
				
				int tmp = -1;
				while( (tmp = bis.read()) != -1 ){
					bos.write(tmp);
				}
				
				bos.flush();
				bos.close();
				bis.close();
				
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return Action.SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

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

	public File[] getPhoto() {
		return photo;
	}

	public void setPhoto(File[] photo) {
		this.photo = photo;
	}

	public String[] getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String[] photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String[] getImgSrc() {
		return imgSrc;
	}

	public void setImgSrc(String[] imgSrc) {
		this.imgSrc = imgSrc;
	}
}

3.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" extends="struts-default" namespace="/">
		<action name="file2" class="cn.acion.FileAction">
			<param name="savePath">/upload</param>
			<result>/success.jsp</result>
		</action>
	</package>
</struts>    


4.上传成功页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'succ.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    姓名:${name } <br>
    年龄:${age }<br/>
  性别:${sex }<br/>
 照片列表:
 	<c:forEach items="${imgSrc}" var="src">
 		<img src="${src }" />
 	</c:forEach>
  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值