文件上传:
三种上传方案
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="width: 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>

struts图片上传的更多相关文章

  1. Java Struts图片上传至指定文件夹并显示图片

    继上一次利用Servlet实现图片上传,这次利用基于MVC的Struts框架,封装了Servlet并简化了JSP页面跳转. JSP上传页面 上传一定要为form加上enctype="mult ...

  2. jsp解决kindeditor在线编辑器struts图片上传问题

    1.下载 官网下载ckeditor,解压后去掉不需要的部分,仅需保留plugin,lang,theme文件夹,这三个文件夹中用不到的东西可以删除, 比如lang文件下存放所有语言文件js,仅仅 保留e ...

  3. CKEditor图片上传实现详细步骤(使用Struts 2)

    本人使用的CKEditor版本是3.6.3.CKEditor配置和部署我就不多说. CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传. ...

  4. struts中用kindeditor实现的图片上传并且显示在页面上

    做公司网站的时候由于需要在内容属性中加入图片,所以就有了这个问题,本来一开始找几篇文章看都是讲修改kindeditor/jsp/file_manager_json.jsp和upload_json.js ...

  5. kindeditor图片上传 struts2实现

    一.kindeditor以及struts2部署搭建不再赘述,如需要请参考kindeditor使用方法 Struts2框架搭建 二.kindeditor图片上传所依赖jar包在kindeditor\js ...

  6. html图片上传阅览并且点击放大

                  关闭   qq_31540195的博客       目录视图 摘要视图 订阅 异步赠书:9月重磅新书升级,本本经典           程序员9月书讯      每周荐书: ...

  7. 大图片上传(ImageIO,注意有的图片不能上传时因为他是tiff格式)

    一下是必要的: 1.enctype="multipart/form-data" 2. //不要使用myeclipse自动生成的get.set方法(struts2中的用法) publ ...

  8. 使用struts2完成ckeditor和图片上传

    代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...

  9. webuploader项目中多图片上传实例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. 这可能是最漂亮的Spring事务管理详解

    事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用 ...

  2. libwebrtc & libmediasoupclient编译

    本文简单介绍在Ubuntu下libwebrtc的编译过程. 由于网速限制,实际编译过程是在远程vps上编译滴. 系统环境 Ubuntu 18.04系统的虚拟主机. root@vultr:~# pwd ...

  3. IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值3

    5  边框属性 边框属性用于设置一个元素的边框风格.边框宽度.边框颜色,可以一起设置4条边的边框,也可对上边框.右边框.下边框和左边框单独设置.分别介绍如下. a.边框风格属性 可以通过边框风格属性b ...

  4. sublime插件开发教程4

    写几个简单的例子详解下 import sublime import sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): d ...

  5. 了解Github

    一.什么是Github Github是全球最大的社交编程及代码托管网站(https://github.com/). Github可以托管各种git库,并提供一个web界面(用户名.github.io/ ...

  6. 剑指offer笔记面试题14----剪绳子

    题目:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,n > 1 并且m > 1),每段绳子的长度记为k[0], k[1], ...k[m].请问k[0] x k[1] x .. ...

  7. 解决Xcode10 Library not loaded: /usr/lib/libstdc++.6造成的crash及报错

    关键字1:dyld: Library not loaded: /usr/lib/libstdc++.6.dylib   Referenced from: 关键字2:Reason: no suitabl ...

  8. 团队项目之团队展示&选题

    团队博文:https://www.cnblogs.com/blackpanda/p/11734448.html 一. 团队展示 1.  队名: Black Panda 2.  队员: 郑伟金 3117 ...

  9. 安装 tensorflow 1.1.0;以及安装其他相似版本tensorflow遇到的问题;tensorflow 1.13.2 cuda-10环境变量配置问题;Tensorflow 指定训练时如何指定使用的GPU;

    # 安装 2.7 环境conda create -n python2. python= conda activate python2. # 安装 1.1.0 gpu版本pip # 配置环境变量expo ...

  10. Linux 打开端口方法

    关闭防火墙:service iptables stop 开启防火墙:service iptables start 防火墙状态:service iptables status 永久关闭:chkconfi ...