zoukankan      html  css  js  c++  java
  • struts图片上传

    文件上传:
    三种上传方案
    1、上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高
    2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
    文件服务器

    3、在数据库表中建立二进制字段,将图片存储到数据库





    package com.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.crud.dao.ClazzDao; import com.crud.entity.Clazz; import com.crud.uitl.BaseAction; import com.crud.uitl.PageBean; public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ private Clazz clz = new Clazz(); private ClazzDao clzDao=new ClazzDao(); private File file; private String fileFileName; private String fileConentType; /** * 跳转上传图片界面 * @return */ public String preUpload() { try { this.result = this.clzDao.list(clz, null).get(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "preUpload"; } public String upload() { String realDir = "D:/tanzhixian"; String severDir="/upload"; try { // FileUtils.copyFile(file, new File(realDir +"/"+fileFileName )); copyFile(file, new File(realDir +"/"+fileFileName)); clz.setPic(severDir+"/"+fileFileName); this.clzDao.edit(clz); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } /** * 缓冲流进行拷贝 * @param source * @param target * @throws IOException */ public void copyFile(File source, File target) throws IOException { BufferedInputStream in =new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target)); byte[] buf = new byte[1024]; int len=0; while((len = in.read(buf)) !=-1) { out.write(buf, 0, len); } in.close(); out.close(); } public String list() { PageBean pageBean = new PageBean(); pageBean.setRequest(request); try { List<Clazz> list = this.clzDao.list(clz, pageBean); request.setAttribute("clzList", list); request.setAttribute("pageBean", pageBean); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "list"; } /** * 跳转界面 * @return */ public String preSave() { if(clz.getCid() !=0) { try { this.result = this.clzDao.list(clz, null).get(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "preSave"; } public String add() { try { this.clzDao.add(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String edit() { try { this.clzDao.edit(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String del() { try { this.clzDao.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 getFileConentType() { return fileConentType; } public void setFileConentType(String fileConentType) { this.fileConentType = fileConentType; } }

      

    <?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.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>
    

      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="/lingerqi" prefix="z" %>
        
    <!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}/sy/clz_list.action"
    		method="post">
    		书名:<input type="text" name="bname"> <input type="submit"
    			value="确定">
    		<!-- <input type="hidden" name="pagination" value="false"> 
    			<input type="hidden" name="rows" value="10"> -->
    	</form>
    	<a href="${pageContext.request.contextPath}/sy/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 src="${pageContext.request.contextPath}${b.pic }" style=" 50px; height: 50px;">
    				</td>
    				<td><a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a> </td>
    			    <td><a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a> </td>
    			    <td><a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a></td>
    			</tr> 
    		</c:forEach>
    	</table>
    	<z:page pageBean="${pageBean}"></z:page>
    </body>
    
    </html>
    

      

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

      在server.xml最后加入:<Context path="/struts/upload" docBase="(路径)" ></Context>

     

  • 相关阅读:
    [Javascript] Use a custom sort function on an Array in Javascript
    [Unit Testing] Fundamentals of Testing in Javascript
    [WASM] Create a New Rust/Webpack Project using the rust-webpack Template
    [Adobe Analytics] Segments types
    win7系统远程连接其它计算机,并且向远程机传输文件
    移动应用数据统计分析平台汇总
    设计模式(策略模式)
    程序员与卓别林
    我的Android进阶之旅------>HTTP 返回状态值详解
    OSX: 真的吗?Mac OS X重大漏洞 改时钟获系统最高权限
  • 原文地址:https://www.cnblogs.com/omji0030/p/11279366.html
Copyright © 2011-2022 走看看