struts 上传文件 Dynavalidatorform 实例
一、相关jar包
一个空struts工程的jar包:
另上传文件的两个jar包:
二、页面
1、上传页面upload.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 action="upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="upload_file" value="" id="upload_file" style="height:20px;">
<input type="submit" id="submitButton" value="提交">
</form>
</body>
</html>
注:a. form表单要有属性 enctype="multipart/form-data"。
b. 上传文件的选择文件input的name属性name="upload_file" 要与下面的form-bean下form-property的name值一致。
2、struts-config.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="upload"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="upload_file" type="org.apache.struts.upload.FormFile" />
</form-bean>
</form-beans> <action-mappings>
<action path="/upload" type="com.UploadAction" name="upload"
scope="request">
<forward name="sucess" path="/sucess.jsp"></forward>
</action>
</action-mappings> <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml" />
</plug-in> </struts-config>
注:第8行 form-bean的name属性值要与第15行action的name值一致。
3、UploadAction.java
package com; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile; public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { DynaActionForm dynaForm = (DynaActionForm) form;
////这个upload_file对应配置form-property 和jsp页面里选择文件input
FormFile file = (FormFile) dynaForm.get("upload_file"); String loalPath = null;
String filePath = null;
if (file != null && !file.toString().trim().equals("")) {
loalPath = file.getFileName();
try {
InputStream in = file.getInputStream();
int length = 0;
String path = this.getServlet().getServletContext().getRealPath("/upload")+ "\\" + loalPath;
File outFile = new File(path);
if (!outFile.getParentFile().exists())
outFile.getParentFile().mkdirs();
if (outFile.exists()) {
outFile.delete();
}
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(outFile));
byte[] buffer = new byte[4096];
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println(filePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return mapping.findForward("sucess");
} }
struts 上传文件 Dynavalidatorform 实例的更多相关文章
- Struts上传文件
Struts上传文件分为两个步骤: 1). 首先将客户端上传的文件保存到Struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servl ...
- Struts 上传文件
1. 客户端注意事项 method="post" enctype="multipart/form-data" <input type="file ...
- struts上传文件 血案
记录一个图片上传之后没有后缀 拓展名问题 平常我们查询数据都是 fileImage=fileImageService.getQuery(); 让entity等于它 那么fileImage.getF ...
- 利用Struts上传文件
在利用struts2完成上传文件到服务器时,遇到获取不到文件名 原因是在Action中的属性名没有和jsp中的属性名匹配 <%@ page language="java" i ...
- java+struts上传文件夹文件
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- struts上传文件报argument type mismatch错误
报错如下图所示: 报错原因:把String 强行转换成FormFile,所以才会抛出argument type mismatch.经查询:表单(html:form)中enctype="mul ...
- Spring Mvc 上传文件Demo 实例
返得利购物. 淘宝.京东500家商城合作,包括全面的商城返利网.注冊就送5元,购物就有返利.随时提现. 同学们,新一轮的返利大潮正在慢慢靠近,让购物都认为自己在赚钱.购物,机票.游戏.酒店旅游,地方特 ...
- http 协议上传文件multipart form-data boundary 说明--转载
原文地址:http://xixinfei.iteye.com/blog/2002017 含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协 ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
随机推荐
- CF959E Mahmoud and Ehab and the xor-MST 思维
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem t ...
- 我的web安全工程师学习之路——规划篇
据网上收集的web安全工程师需要掌握哪些技能,职位要求以及如何入门,加上学习网易推出的web安全工程师微专业课程,为了进一步学习,所以给自己做了一些小小规划,也希望给同样想成为web安全工程师的同仁们 ...
- 实施MySQL ReplicationDriver支持读写分离
MySQL 提供支持读写分离的驱动类: com.mysql.jdbc.ReplicationDriver 替代 com.mysql.jdbc.Driver 注意,所有参数主从统一: jdbc:mysq ...
- A. Cinema Line
A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Rabbitmq相关学习网址
1.安装文档: http://www.cnblogs.com/shuzhenyu/p/9823324.html 2.RabbitMq的整理 exchange.route.queue关系 https:/ ...
- jsonp跨域请求及本质
在html页面中,能实现跨域请求的是 第一: <script src="http://localhost:59602/JsonpTest.ashx?callBack=callBack& ...
- sql函数将1,2,3转换为表
/****** Object: UserDefinedFunction [dbo].[splitstring_to_table] Script Date: 2017/7/11 9:35:58 **** ...
- Spark机器学习库(MLlib)官方指南手册中文版
中文https://blog.csdn.net/liulingyuan6/article/details/53582300 https://yq.aliyun.com/articles/608083 ...
- prototype.js
(1)$() 方法是在DOM中使用过于频繁的 document.getElementById() 方法的一个便利的简写, 就像这个DOM方法一样,这个方法返回参数传入的id的那个元素. (2)
- AndroidStudio打包jar
1.像平常一个样新建一个项目 2.(在步骤1的基础上)点击File-->New-->New Module—>选择Android Library-->点击Next(如下图:) 定 ...