文件上传以及底层代码
文件上传有三种方式:
1、上传到tomcat服务器
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器
3、在数据库表中建立二进制字段,将图片存储到数据库
具体的思路:
1、完成功能
2、两个底层
今天的demo是在我上一篇的博客的基础上编辑。
在web层中代码如下;
我把关于上传功能的底层代码也写在下面了
/**
* 直接上传图片
* @return
*/
public String upload() {
try {
//实际图片存储的地址
String targetDir ="H:/aa";
//存到数据库中的地址
String severPath = "/upload";
// FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName));
copyBufFile(file, new File(targetDir+"/"+fileFileName));
//注意:数据库存放的是网络请求地址 ,而不是本地图片的请求地址
clz.setPic(severPath+"/"+fileFileName);
this.clzdao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* FileUtils.copyFile的底层,并且通过缓冲区进行增强
* @param source
* @param tafget
*/
public void copyBufFile(File source,File tafget){
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tafget));
byte[] bbuf = new byte[1024];
int len =0;
while((len = in.read(bbuf))!=-1) {
out.write(bbuf,0,len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 跳转文件上传页面
* @return
*/
public String preUpload() {
try {
Clazz c = this.clzdao.list(clz, null).get(0);
request.setAttribute("clz", c);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toUpload";
}
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 id="bookForm" action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="cid" value="${clz.cid }"><br>
<input type="hidden" name="cname" value="${clz.cname }"><br>
<input type="hidden" name="price" value="${clz.cteacher }"><br>
<input type="file" name="file">
<input type="submit" value="提交" ><br>
</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" namespace="/sy">
<interceptors>
<interceptor name="one" class="com.wt.crud.interceptor.OneInterceptor"></interceptor>
<interceptor name="two" class="com.wt.crud.interceptor.TwoInterceptor"></interceptor>
</interceptors>
<action name="/demo_*" class="com.wt.web.HelloAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="/stack_*" class="com.wt.test.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="/clz_*" class="com.wt.crud.web.ClazzAction" method="{1}">
<interceptor-ref name="one"></interceptor-ref>
<interceptor-ref name="two"></interceptor-ref>
<result name="list">/clazzlist.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
<result name="toUpload">/upload.jsp</result>
</action>
</package>
</struts>
至此功能实现了,接下来我写关于拦截器的demo
首先创建interceptor;
其中写两个类,OneInterceptor以及TwoInterceptor
其中的代码两个基本相似;
package com.wt.crud.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class OneInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("========OneInterceptor========1");
String invoke = arg0.invoke();
System.out.println("========OneInterceptor========2");
return invoke;
}
}
关于配置的问题我在上面也写了,至此我今天的demo写好了。