其他分享
首页 > 其他分享> > struts2完成图片上传

struts2完成图片上传

作者:互联网

文章目录

图片上传

今天在上次增删查改的基础上把图片上传完善,要达到的效果:把图片上传到服务器并保存到本地,并且在jsp页面展示出来
在这里插入图片描述

三种上传方案

1、上传到tomcat服务器
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器	
3、在数据库表中建立二进制字段,将图片存储到数据库

今天我们介绍的是第二种方式,并且了解它的原理
实现代码
upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<form action="${pageContext.request.contextPath}/sy/clz_Upload.action" method="post" enctype="multipart/form-data">
		<input type="hidden" name="cid" value="${clz.cid }" /><br/>
		<input type="hidden" name="cname" value="${clz.cname }" /><br/>
		<input type="hidden" name="cteacher" value="${clz.cteacher }" /><br/>
		<input type="file" name="file" />
		<input type="submit" />
	</form>

</body>
</html>

同样在我们的struts.xml中配置好

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sy" extends="base" namespace="/sy">
		<action name="/demo_*" class="com.xy.web.DemoAction" method="{1}" >
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/stack_*" class="com.xy.test.DemoAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/clz_*" class="com.xy.crud.web.ClazzAction" method="{1}">
			<result name="list">/clzList.jsp</result>
			<result name="preSave">/clzEdit.jsp</result>
			<result name="toList" type="redirectAction" >/clz_list</result>
			<result name="toUpload">/upload.jsp</result>
		</action>
	</package>
</struts>

ClazzAction控制器,我们先是用struts的完成了图片上传,然后再增加增加写了一个缓冲流的文件上传
1、先定义好文件的属性,文件类型,文件名
2、接收文件和选择保存文件的路径
3、修改数据库中的图片路径字段,改成服务器能读到的路径

package com.xy.crud.web;

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.sql.SQLException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.xy.crud.dao.ClazzDao;
import com.xy.crud.entity.Clazz;
import com.xy.crud.util.BaseAction;
import com.xy.crud.util.PageBean;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {

	private ClazzDao clzDao = new ClazzDao();
	private Clazz clz = new Clazz();
	
	private File file;
	private String fileContentType;
	private String fileFileName;
	
	
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	
	
	/**
	 * 上传图片
	 * @return
	 */
	public String Upload() {
		try {
//			注意:在Linux下没有E盘的,Linux下只有哦一个盘符,那么意味着,当打包到Linux服务器的时候需要改动代码
//			这个时候通常是这么解决的,将targetPath对应目录串,配置到资源文件中,通过Properties类进行动态读取
//			那么当需要将项目发布到Linux服务器的时候,只需要改变xxx.properties文件中的targetPath=/xxx/img/
			
//			实际图片存储的位置
			String targetDir="F:/img";
//			存到数据中的地址
			String serverPath="/uploads";
//			FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName));
			copyBufFile(file, new File(targetDir+"/"+fileFileName));
//			注意:数据库存放的是网络请求地址,而不是本地储存地址
			clz.setPic(serverPath+"/"+fileFileName);
			this.clzDao.edit(clz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "toList";
	}
	
	/**
	 * FileUtils.copyFile的底层,并且通过缓冲区进行了增强
	 * @param source
	 * @param target
	 * @throws FileNotFoundException
	 */
	public void copyBufFile(File source,File target) throws FileNotFoundException {
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(source));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
		byte[] bbuf=new byte[1024];
		int len=0;
			try {
				while((len = in.read(bbuf))!=-1) {
						out.write(bbuf,0,len);
					}
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			} 
	}
	
	/**
	 * 跳转文件上传页面
	 * @return
	 */
	public String preUpload() {
		if(clz.getCid()!=0) {
			Clazz z;
			try {
				z = this.clzDao.list(clz, null).get(0);
				request.setAttribute("clz", z);
			} catch (IllegalArgumentException | IllegalAccessException | InstantiationException | SQLException e) {
				e.printStackTrace();
			}
		}
		return "toUpload";
	}
	
	
	

	/**
	 * 查所有
	 * 
	 * @return
	 * @throws SQLException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public String list() throws IllegalArgumentException, IllegalAccessException, InstantiationException, SQLException {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		List<Clazz> list = this.clzDao.list(clz, pageBean);
		request.setAttribute("clzList", list);
		request.setAttribute("pageBean", pageBean);
		return "list";
	}

	/**
	 * 跳转新增修改页面的公用方法
	 * 
	 * @return
	 * @throws SQLException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public String preSave() throws IllegalArgumentException, IllegalAccessException, InstantiationException, SQLException {
		if(clz.getCid()!=0) {
			Clazz z = this.clzDao.list(clz, null).get(0);
			request.setAttribute("clz", z);
		}
		return "preSave";
	}

	public String edit() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		this.clzDao.edit(clz);
		return "toList";
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	public String add() {
		try {
			result=this.clzDao.add(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public String del() {
		try {
			this.clzDao.del(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	@Override
	public Clazz getModel() {
		return clz;
	}
}

最后在你的本地servers的server.xml加一条代码就可以了

<Context path="/T224_struts/upload" docBase="E:/temp/T224/"/>

拦截器Interceptor

两种实现拦截器的方法

implements Interceptor
extends AbstractInterceptor

与filter的区别:先过filter再过interceptor
定义拦截器,在你的struts.xml文件中加上interceptor,并且配置好服务

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sy" extends="base" namespace="/sy">
		 <interceptors>
			<interceptor name="one" class="com.xy.crud.interceptor.OneInterceptor"></interceptor>
			<interceptor name="two" class="com.xy.crud.interceptor.TwoInterceptor"></interceptor>
		</interceptors> 
		
	
		<action name="/demo_*" class="com.xy.web.DemoAction" method="{1}" >
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/stack_*" class="com.xy.test.DemoAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/clz_*" class="com.xy.crud.web.ClazzAction" method="{1}">
		 <interceptor-ref name="one"></interceptor-ref>
			<interceptor-ref name="two"></interceptor-ref>
			<result name="list">/clzList.jsp</result>
			<result name="preSave">/clzEdit.jsp</result>
			<result name="toList" type="redirectAction" >/clz_list</result>
			<result name="toUpload">/upload.jsp</result>
		</action>
	</package>
</struts>

标签:return,String,throws,struts2,clz,import,上传,public,图片
来源: https://blog.csdn.net/qq_43517653/article/details/95505510