其他分享
首页 > 其他分享> > 文件上传

文件上传

作者:互联网

文件上传:

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

虽然有三种上传方案,但我们用的是第二种
ClazzAction

package com.Liuyujian.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.Liuyujian.crud.dao.ClazzDao;
import com.Liuyujian.crud.entity.Clazz;
import com.Liuyujian.crud.util.BaseAction;
import com.Liuyujian.crud.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {
 private Clazz clz=new Clazz();
 private ClazzDao dao=new ClazzDao();
 //这里的属性名要跟name对应     xxx
 private File file;
 //xxxFilename
 private String fileFileName;
 // xxxContetType
 private String fileContetType;
 
 /**
  * 跳转上传图片的页面
  * @return
  */
 public String preUpload() {
		 try {
			this.result=this.dao.list(clz, null).get(0);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 return "preUpload";
 }
 
 
 public String Upload() {
	String resDir="D:/222";
	String severDir="/upload";
	try {
		
		FileUtils.copyFile(file,new File(resDir +"/"+ fileFileName));
		System.out.println(fileFileName);
	    clz.setPic(severDir+"/"+ fileFileName);
//	    copyFile(file, new File(resDir +"/"+ fileFileName));   
	    this.dao.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[] nnuf =new byte[1024];
//	int len=0;
//	while((len =in.read(nnuf))!=-1) {
//		out.write(nnuf,0,len);
//		
//	}
//	in.close();
//	out.close();
//	
// }
 
 
 /**
  * 查询方法
  * @return
  */
 public String list() {
	 PageBean pageBean=new PageBean();
	 pageBean.setRequest(request);
	 System.out.println("11111111");
     try {
		List<Clazz> list = this.dao.list(clz, pageBean);
		request.setAttribute("clzList", list);
	    request.setAttribute("pageBean", pageBean);
     } catch (InstantiationException | IllegalAccessException | SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
     return "list";
 }
 /**
  * 跳转编辑页面(新增修改页面)
  * @return
  */
 public String preSave() {
	 if(clz.getCid() !=0) {
		 try {
			this.result=this.dao.list(clz, null).get(0);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 return "preSave";
 }
 /**
  * 增加方法
  * @return
  */
 public String add() {
	 try {
		this.code=this.dao.add(clz);
	} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
			| SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 return "toList";
	 }
 /**
  * 删除方法
  * @return
  */
 public String del() {
	 try {
		this.code=this.dao.del(clz);
	} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
			| SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 return "toList";
	 }
 /**
  * 修改方法
  * @return
  */
 public String edit() {
	 try {
		this.code=this.dao.edit(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 getFileContetType() {
	return fileContetType;
}
public void setFileContetType(String fileContetType) {
	this.fileContetType = fileContetType;
}



}

struts-sy.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" >	
	<action name="/clz_*" class="com.Liuyujian.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>

clazzList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="/thf" prefix="t" %>
<!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>
<body>
<h2>小说目录</h2>
	<br>

	<form action="${pageContext.request.contextPath}/clz_list.action"
		method="post">
		书名:<input type="text" name="cname"> <input type="submit"
			value="确定">
			<input type="hidden" name="rows" value="15">
	</form>
	<a href="${pageContext.request.contextPath}/clz_preSave.action">增加</a>
	<table border="1" width="100%">
		<tr>
			<td>编号</td>
			<td>班级名称</td>
			<td>教员</td>
			<td>图片</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${clzList }" var="b">
			<tr>
				<td>${b.cid }</td>
				<td>${b.cname}</td>
				<td>${b.cteacher}</td>
				<td>
				<img style="height: 80px ; width:  80px" src="${pageContext.request.contextPath}${b.pic}">
				</td>
				<td>
					<a href="${pageContext.request.contextPath}/clz_preSave.action?cid=${b.cid}">修改</a>&nbsp;
					<a href="${pageContext.request.contextPath}/clz_del.action?cid=${b.cid}">删除</a>&nbsp;
					<a href="${pageContext.request.contextPath}/clz_preUpload.action?cid=${b.cid}">图片上传</a>&nbsp;
				</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

clazzUpload.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}/clz_Upload.action " method="post" enctype="multipart/form-data">
   <input type="hidden" value="${result.cid }" name="cid"><br>
  <input type="hidden" value="${result.cname }" name="cname"><br>
 <input type="hidden" value="${result.cteacher }" name="cteacher"><br>
 <input type="file" name="file">
   <input type="submit" value="ok">
</form>
</body>
</html>

最后需要修改 servers 里面的配置文件 server.xml 在最后加上

<Context path="/T226_struts1/upload" docBase="D:/222/"/>

在这里插入图片描述
页面显示效果
在这里插入图片描述

标签:文件,return,String,clz,catch,import,上传,public
来源: https://blog.csdn.net/weixin_45180919/article/details/97899069