在struts2框架做上传功能

来源:互联网 发布:淘宝宝贝过期不存在 编辑:程序博客网 时间:2024/06/09 15:53

上传一张图片

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
原创粉丝点击