基于struts2的文件上传功能

版权声明:努力去改变!!! https://blog.csdn.net/lq1759336950/article/details/88543373

一:struts2的文件上传的步骤:
1、设置表单属性中enctype=“multipart/form-data”
2、表单元素中

3、在Action类的属性中必须存在三个属性,分别为:
private File 文件类型属性名;
private String 文件类型属性名ContentType;
private String 文件类型属性名FileName;
4、上传文件
FileUtils.copyFile(文件类型属性名,服务器端放置上传文件的目标路径);
二:具体代码如下(以上传图片为例):
实体类Photo:

package com.bean;//此处包名要与你自己的一致
import java.io.*;
public class Photo implements Serializable {
	private String uname;
	private String title;
	private String fname;
	
	/***上传文件相关的属性*********/
	private File pic;
	private String picContentType;
	private String picFileName;
	/**************************/
	public Photo() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Photo(String uname, String title, String fname, File pic,
			String picContentType, String picFileName) {
		super();
		this.uname = uname;
		this.title = title;
		this.fname = fname;
		this.pic = pic;
		this.picContentType = picContentType;
		this.picFileName = picFileName;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public File getPic() {
		return pic;
	}
	public void setPic(File pic) {
		this.pic = pic;
	}
	public String getPicContentType() {
		return picContentType;
	}
	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}
	public String getPicFileName() {
		return picFileName;
	}
	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}
}

PhotoAction动作模型的类:

package com.action;
import com.bean.*;

import java.io.*;
import java.util.*;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class PhotoAction {
	private Photo photo;
	private String path;
	public Photo getPhoto() {
		return photo;
	}
	public void setPhoto(Photo photo) {
		this.photo = photo;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	
	
	public String upload(){
		//获取服务器的物理路径
		String realpath=ServletActionContext.getServletContext().getRealPath("/");
		HttpSession session=ServletActionContext.getRequest().getSession();
		List<Photo> lspt=(List<Photo>) session.getAttribute("lspt");
		if(lspt==null){
			lspt=new ArrayList<Photo>();
		}
		
		//获取上传文件的名称
		String fname=photo.getPicFileName();
		if(fname.lastIndexOf(".")!=-1){//判断是否存在后缀
			//获取后缀名称
			String ext=fname.substring(fname.lastIndexOf("."));
			if(ext.equalsIgnoreCase(".jpg")){
				//改名
				String newfname=new Date().getTime()+ext;
				//创建文件对象,设置上传文件存储的路径
				File destFile=new File(realpath+"/uppic/"+newfname);
				
				try {
					FileUtils.copyFile(photo.getPic(), destFile);//存放图片到指定路径
					photo.setFname(newfname);
					lspt.add(photo);
					session.setAttribute("lspt", lspt);
					path="show.jsp";
					return "ok";
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path="index.jsp";
		return "fail";
	}
}

upload.jsp页面主要代码:

<form action="upload_Photo.action" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="550" border="1" align="center" cellpadding="1" cellspacing="0">
    <tr>
      <td colspan="2" align="center" bgcolor="#D6D6D6">上传图片</td>
    </tr>
    <tr>
      <td width="138">上传者姓名</td>
      <td width="402"><label for="photo.uname"></label>
      <input type="text" name="photo.uname" id="photo.uname" /></td>
    </tr>
    <tr>
      <td>图片标题</td>
      <td><input type="text" name="photo.title" id="photo.title" /></td>
    </tr>
    <tr>
      <td>选择文件</td>
      <td><label for="photo.pic"></label>
      <input type="file" name="photo.pic" id="photo.pic" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#D6D6D6"><input type="submit" name="button" id="button" value="提交" />
      <input type="reset" name="button2" id="button2" value="重置" /></td>
    </tr>
  </table>
  <p align="center"><a href="show.jsp">查看图片列表</a></p>
</form>

show.jsp页面主要代码:

<body>
<p align="center">图片列表
</p>
<hr width="700" />
<table width="650" border="1" align="center" cellpadding="1" cellspacing="0">
  <tr align="center" bgcolor="#FFFF99">
    <td width="226">上传者姓名</td>
    <td width="240">图片标题</td>
    <td width="170">预览</td>
  </tr>
  <c:forEach items="${lspt}" var="pt">
  <tr align="center">
    <td>${pt.uname}</td>
    <td>${pt.title}</td>
    <td><a href="uppic/${pt.fname}"><img src="uppic/${pt.fname}" width="80" height="70" alt="" /></a></td>
  </tr>
  </c:forEach>
</table>
<hr width="700" />
<p align="center"><a href="upload.jsp">返回上传</a></p>
<p>&nbsp;</p>
</body>

struts.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>
	<constant name="struts.ui.them" value="simple"></constant>
	<package name="users" namespace="/" extends="struts-default">
		<action name="*_*" class="com.action.{2}Action" method="{1}">
			<result name="ok" type="redirect">${path}</result>
		</action>
	</package>
</struts>    

运行结果:
在这里插入图片描述
在这里插入图片描述
点击预览图片可放大喔!

猜你喜欢

转载自blog.csdn.net/lq1759336950/article/details/88543373
今日推荐