在struts2框架做上传功能

上传一张图片

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="file1" enctype="multipart/form-data" method="post">
    	姓名:<input name="name" /></br>
    	年龄:<input name="age" /></br>
    	性别:<input name="sex" /></br>
    	照片:<input name="photo" type="file" /></br>
    	<input name="submit" type="submit" /></br>
    </form>
  </body>
</html>


2.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="file1" class="cn.action.FileAction" method="execute">
			<param name="savePath">/upload</param>
			<result>succ.jsp</result>
		</action>
		
	</package>
</struts>    

3.FileAction.java


package cn.action;

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(){
		//下两行获取路径,savepath在xml中要配置
		ServletContext context=ServletActionContext.getServletContext();
		String realPath=context.getRealPath(savePath);
		//查看是否存在文件路径
		File saveDir=new File(realPath);
		if(!saveDir.exists()){
			saveDir.mkdir();
		}
		//随机数,做图片名
		String saveFileName = UUID.randomUUID().toString();
		//获得图片格式
		String ext = photoFileName.substring(photoFileName.lastIndexOf("."));
		//上传到的目的地的地址
		String outFilePath = realPath + "/" + saveFileName + ext;
		
		this.imgSrc = this.savePath + "/" + saveFileName + ext;
		// ȥ�� ��ͷ�� / 
		this.imgSrc = imgSrc.substring(1);
		
		
		try {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(photo));
			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;
	}
	
}

4.上传成功后显示页面

<%@ 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 '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/>
 照片:<img src="${imgSrc }" />
  </body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值