struts文件上传
生活随笔
收集整理的這篇文章主要介紹了
struts文件上传
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文件上傳的三種方案:
1、上傳到tomcat服務(wù)器 (上傳圖片的存放位置與tomcat服務(wù)器的耦合度太高,會導(dǎo)致系列的問題)
2、在數(shù)據(jù)庫表中建立二進(jìn)制字段,將圖片存儲到數(shù)據(jù)庫(會導(dǎo)致數(shù)據(jù)庫的表數(shù)據(jù)量過大)
3、上傳到指定文件目錄,添加服務(wù)器與真實目錄的映射關(guān)系,從而解耦上傳文件與tomcat的關(guān)系
文件服務(wù)器
由于前兩種方法都存在比較大的缺陷,日漸被淘汰,我為大家展示第三種上傳方法:
在上次struts項目的基礎(chǔ)上完善主界面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="z" uri="/myjstl"%> <!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><h2>小說目錄</h2><br><form action="${pageContext.request.contextPath}/sy/clz_list.action"method="post">書名:<input type="text" name="bname"> <input type="submit"value="確定"></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></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: 50px" src="${pageContext.request.contextPath}${b.pic}"></td><td><ahref="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a> <ahref="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">刪除</a> <ahref="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">圖片上傳</a> </td></tr></c:forEach></table><z:page pageBean="${pageBean }"></z:page> </body> </html>添加上傳頁面clzUpload.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>圖片上傳</title> </head> <body> <form action="${pageContext.request.contextPath}/sy/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>配置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="/hello_*" class="com.temp.HelloAction" method="{1}"><result name="success">/success.jsp</result></action><action name="/clz_*" class="com.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>創(chuàng)建ClazzAction.java
package com.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.dao.ClazzDao; import com.entity.Clazz; import com.util.BaseAction; import com.util.PageBean; import com.opensymphony.xwork2.ModelDriven;public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{private Clazz clz=new Clazz();private ClazzDao clzDao=new ClazzDao();//這里的屬性名要和name對應(yīng)private File file;//xxxFileNameprivate String fileFileName;//xxxContentTypeprivate String fileContentType;/*** 跳轉(zhuǎn)上傳圖片的界面* @return*/public String preUpload() {try {this.result= this.clzDao.list(clz, null).get(0);} catch (InstantiationException | IllegalAccessException | SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}return "preUpload";}public String upload() {String realDir="D:/myimg";String severDir="/upload";try {FileUtils.copyFile(file, new File(realDir+"/"+fileFileName));//copyFile(file,new File(realDir+"/"+fileFileName));clz.setPic(severDir+"/"+fileFileName);try {this.clzDao.edit(clz);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}return "toList";}/*** 利用緩沖流技術(shù)進(jìn)行拷貝* @param source* @param target* @throws IOException */public void copyFile(File source,File target) throws IOException {BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));byte[] bbuf=new byte[1024];int len=0;while((len=in.read(bbuf))!=-1) {out.write(bbuf,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 (InstantiationException | IllegalAccessException | SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}return "list";}/*** 跳轉(zhuǎn)編輯頁面 (新增修改頁面)* @return*/public String preSave() {if(clz.getCid()!=0) {try {this.result= this.clzDao.list(clz, null).get(0);} catch (InstantiationException | IllegalAccessException | SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}}return "preSave";}public String add() {try {this.code= 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";}@Overridepublic Clazz getModel() {// TODO Auto-generated method stubreturn 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;}}然后配置Servers中的serser.xml文件
<Context path="/struts(項目名)/upload" docBase="D:/myimg(用來存儲圖片的文件夾的路徑)"/>
?
效果如下:
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/huxiaocong/p/11285976.html
總結(jié)
以上是生活随笔為你收集整理的struts文件上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmeter插件 --PerfMon M
- 下一篇: Sharepoint 2010 新特性笔