Stuts 文件上传
三种上传方案
1、上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器
3、在数据库表中建立二进制字段,将图片存储到数据库
今天我们利用2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器来进行文件的上传
首先新建一个图片上传的jsp界面
clzUpload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data"> 11 <input type="hidden" value="${result.cid }" name="cid"><br> 12 <input type="hidden" value="${result.cname }" name="cname"><br> 13 <input type="hidden" value="${result.cteacher }" name="cteacher"><br> 14 <input type="file" name="file"> 15 <input type="submit"> 16 </form> 17 </body> 18 </html>
web层ClazzAction
//属性名要和name对应 xxx
private File file;
//xxxFileName
private String fileFileName;
//xxxContentType
private String fileContentType;
跳转上传图片的界面
1 package com.crud.web; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.util.List; 10 11 import org.apache.commons.io.FileUtils; 12 13 import com.crud.dao.Clazzdao; 14 import com.crud.entity.Clazz; 15 import com.crud.util.BaseAction; 16 import com.crud.util.PageBean; 17 import com.opensymphony.xwork2.ModelDriven; 18 19 public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ 20 21 private Clazz clz = new Clazz(); 22 private Clazzdao clzdao = new Clazzdao(); 23 24 //属性名要和name对应 xxx 25 private File file; 26 //xxxFileName 27 private String fileFileName; 28 //xxxContentType 29 private String fileContentType; 30 31 /** 32 * 跳转上传图片的界面 33 * @return 34 */ 35 public String preUpload() { 36 try { 37 this.result = this.clzdao.list(clz, null).get(0); 38 } catch (Exception e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 43 return "preUpload"; 44 } 45 46 public String upload() { 47 String realDri = "E:/Project"; 48 String severDir = "/upload"; 49 try { 50 //FileUtils.copyFile(file, new File(realDri + "/"+fileFileName)); 51 copyFile(file,new File(realDri+"/"+fileFileName)); 52 clz.setPic(severDir+"/"+fileFileName); 53 this.clzdao.edit(clz); 54 } catch (IOException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } catch (NoSuchFieldException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } catch (Exception e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 65 return "toList"; 66 } 67 /** 68 * 缓冲流 69 * @param source 70 * @param target 71 * @throws IOException 72 */ 73 public void copyFile(File source,File target) throws IOException { 74 BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); 75 BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target)); 76 byte[] bbuf=new byte[1024]; 77 int len=0; 78 while((len=in.read(bbuf))!=-1) { 79 out.write(bbuf,0,len); 80 } 81 in.close(); 82 out.close(); 83 } 84 85 public String list() { 86 87 PageBean pageBean = new PageBean(); 88 pageBean.setRequest(request); 89 try { 90 List<Clazz> list = this.clzdao.list(clz, pageBean); 91 92 request.setAttribute("clzList", list); 93 request.setAttribute("pageBean", pageBean); 94 } catch (Exception e) { 95 // TODO Auto-generated catch block 96 e.printStackTrace(); 97 } 98 return "list"; 99 } 100 101 /** 102 * 跳转编辑页面 103 * @return 104 */ 105 public String preSave() { 106 if(clz.getCid() != 0) { 107 try { 108 this.result = this.clzdao.list(clz, null).get(0); 109 } catch (Exception e) { 110 // TODO Auto-generated catch block 111 e.printStackTrace(); 112 } 113 } 114 115 return "preSave"; 116 } 117 118 public String add() { 119 System.out.println("add"); 120 try { 121 this.code = this.clzdao.add(clz); 122 } catch (Exception e) { 123 // TODO Auto-generated catch block 124 e.printStackTrace(); 125 } 126 127 return "toList"; 128 } 129 130 public String edit() { 131 try { 132 this.clzdao.edit(clz); 133 } catch (Exception e) { 134 // TODO Auto-generated catch block 135 e.printStackTrace(); 136 } 137 return "toList"; 138 } 139 140 public String del() { 141 try { 142 this.clzdao.del(clz); 143 } catch (Exception e) { 144 // TODO Auto-generated catch block 145 e.printStackTrace(); 146 } 147 return "toList"; 148 } 149 150 public File getFile() { 151 return file; 152 } 153 154 public void setFile(File file) { 155 this.file = file; 156 } 157 158 public String getFileFileName() { 159 return fileFileName; 160 } 161 162 public void setFileFileName(String fileFileName) { 163 this.fileFileName = fileFileName; 164 } 165 166 public String getFileContentType() { 167 return fileContentType; 168 } 169 170 public void setFileContentType(String fileContentType) { 171 this.fileContentType = fileContentType; 172 } 173 174 @Override 175 public Clazz getModel() { 176 // TODO Auto-generated method stub 177 return clz; 178 } 179 }
以及利用缓存流技术来进行拷贝 也可以直接一行代码来进行
1 public String upload() { 2 String realDri = "E:/Project"; 3 String severDir = "/upload"; 4 try { 5 FileUtils.copyFile(file, new File(realDri + "/"+fileFileName)); 6 7 clz.setPic(severDir+"/"+fileFileName); 8 this.clzdao.edit(clz); 9 } catch (IOException e) { 10 // TODO Auto-generated catch block 11 e.printStackTrace(); 12 } catch (NoSuchFieldException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } catch (Exception e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 }
配置server的server.xml
修改显示数据的jsp界面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="/zking" prefix="z" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <h2>小说目录</h2> 13 <br> 14 <form action="${pageContext.request.contextPath}/sy/clz_list.action" 15 method="post"> 16 书名:<input type="text" name="bname"> <input type="submit" 17 value="确定"> 18 <input type="hidden" name="bname"> 19 </form> 20 <a href="${pageContext.request.contextPath}/sy/clz_preSave.action">增加</a> 21 <table border="1" width="100%"> 22 <tr> 23 <td>编号</td> 24 <td>名称</td> 25 <td>教员</td> 26 <td>图片</td> 27 <del>操作</del> 28 </tr> 29 <c:forEach items="${clzList }" var="b"> 30 <tr> 31 <td>${b.cid }</td> 32 <td>${b.cname }</td> 33 <td>${b.cteacher }</td> 34 <td> 35 <img style="height: 50px; 55px" src="${pageContext.request.contextPath}${b.pic }"> 36 </td> 37 <td> 38 <a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a> 39 <a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a> 40 <a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">文件上传</a> 41 42 43 </td> 44 </tr> 45 </c:forEach> 46 </table> 47 48 <z:page pageBean="${pageBean }"></z:page> 49 50 51 52 53 54 </body> 55 </html>
效果