struts图片上传

文件上传:

1、上传到tomcat服务器
缺点:上传图片的存放位置与tomcat服务器的耦合度太高,上传图片后,需要刷新页面才能查看图片,而且当你的tomcat服务器关闭之后,页面就看不到图片了
2、在数据库表中建立二进制字段,将图片存储到数据库
由于图片内存过高,导致数据过多的时候,一张表的内存过大
3、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器

图片上传界面

<%@ 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>图片上传</title>
</head>
<form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data">
	<input type="hidden" name="cid" value="${result.cid }" >
	<input type="hidden" name="cname" value="${result.cname }" >
	<input type="hidden" name="cteacher" value="${result.cteacher }" >
	<input type="file" name="file" >
	<input type="submit" value="ok" >
</form>
</html>

在这里插入图片描述
ClazzAction控制器

package com.qukang.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 javax.imageio.stream.FileImageInputStream;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.qukang.crud.dao.ClazzDao;
import com.qukang.crud.entity.Clazz;
import com.qukang.crud.uitl.BaseAction;
import com.qukang.crud.uitl.PageBean;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
	private Clazz clz=new Clazz();
	private ClazzDao clazzDao=new ClazzDao();
//	图片路径
//	这个属性名必须要和name对应xxx
	private File file;
//	图片名
	private String fileFileName;
//	图片类型
	private String fileContentType;
	/**
	 * 跳转上传图片的页面
	 * @return
	 */
	public String preUpload() {
		try {
			this.result= this.clazzDao.list(clz,null).get(0);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "preUpload";
	}
	
	public String upload() {
//图片上传的地址
		String realDir="F:/T226";
//服务器的图片地址
		String severDir="/upload";
		try {
			copyFile(file, new File(realDir+"/"+fileFileName) );
//			FileUtils.copyFile(file,new File(realDir+"/"+fileFileName));
			clz.setPic(severDir+"/"+fileFileName);
			this.clazzDao.edit(clz);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "toList";
	}
	/**
	 * 利用缓冲流技术进行拷贝
	 * @param soure
	 * @param target
	 * @throws IOException 
	 */
	public void copyFile(File soure,File target) throws IOException {
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(soure));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
		byte[] buff=new byte[1024];
		int len=0;
		while((len=in.read(buff))!=-1) {
			out.write(buff,0, len);
		}
		in.close();
		out.close();
	}
	public String list() {
		PageBean pageBean=new PageBean();
		pageBean.setRequest(request);
		try {
			List<Clazz> list = this.clazzDao.list(clz, pageBean);
			request.setAttribute("clazzlist",list);
			request.setAttribute("pageBean",pageBean);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "list";
		
	}
	/**
	 * 跳转编辑页面(新增修改页面)
	 */
	public String preSave() {
		if(clz.getCid()!=0) {
			try {
				this.result= this.clazzDao.list(clz,null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "preSave";
	}
	public String add() {
		try {
			this.clazzDao.add(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	
	public String edit() {
		try {
			this.clazzDao.edit(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	public String del() {
		try {
			this.clazzDao.del(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	@Override
	public Clazz getModel() {
		// TODO Auto-generated method stub
		return clz;
	}

	public File getFile() {
		return file;
	}

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

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

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

系统模块
根据控制器返回的结果码进行网业跳转

<?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="/clz_*" class="com.qukang.crud.web.ClazzAction" method="{1}">
		<result name="list">/clzList.jsp</result>
		<result name="preSave">/clzEdit.jsp</result>
		<result name="preUpload">/clzUpload.jsp</result>
		<result name="toList" type="redirectAction">/clz_list</result>
	</action>
	</package>
</struts>

添加服务器与真实目录的映射关系
在这里插入图片描述
主界面效果图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值