struts文件上传(单文件)
第01步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第02步:编写action类
package com.self.action; import java.io.File; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; /**文件上传**/
public class FileUpload {
private File image;//得到上传的文件
private String imageFileName;//获取上传文件的名称,命名规则:页面属性名+FileName
private String imageContentType;//得到上传文件的类型 public FileUpload() {
} /**上传方法**/
public String uploadFile(){
try {
String realPath=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println("工程路径(/images路径):"+realPath);
if(image!=null){
System.out.println("已经获得上传文件!");
File parentFile=new File(realPath);//指定文件保存路径
if(!parentFile.exists()){//保存路径不存在,则创建
parentFile.mkdirs();
}
File saveFile=new File(parentFile,imageFileName);//parentFile:保存路径,imageFileName:保存文件名
FileUtils.copyFile(image, saveFile);//将上传的image复制到saveFile中
ActionContext.getContext().put("message", "保存成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return "tsuccess";
} public File getImage() {
return image;
} public void setImage(File image) {
this.image = image;
} public String getImageFileName() {
return imageFileName;
} public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
} public String getImageContentType() {
return imageContentType;
} public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
} }
第03步:配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<constant name="struts.action.extension" value="do,action"/> <package name="transform" namespace="/" extends="struts-default">
<action name="list_*" class="com.self.action.FileUpload" method="{1}">
<result name="tsuccess">
/outdata.jsp
</result>
</action>
</package>
</struts>
第04步:编写界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form method="post" action="list_uploadFile.action" enctype="multipart/form-data">
文件:<input type="file" name="image">
<BR>
<input type="submit" value="上传文件">
</form>
</body>
</html>
第05步:注意事项
保存路径:控制台输出有,
form表单方法:method="post",
form表单类型:enctype="multipart/form-data",
form表单input名字:同action里面属性名"image"
文件过大,会出异常,需要在struts配置里修改属性
需要导入包:

struts文件上传(单文件)的更多相关文章
- Struts2之文件上传(单文件/多文件)
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)
准备工作: 需要先搭建一个spirngmvc的maven项目 1.加入jar包 <dependency> <groupId>commons-fileupload</gro ...
- 基于spring的文件上传--单文件上传
Spring配置文件 <bean id="multipartResolver" class="org.springframework.web.multipart.c ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- webAPI文件上传时文件过大404错误的问题
背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...
- [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...
- 【文件上传】文件上传的form表单提交方式和ajax异步上传方式对比
一.html 表单代码 …… <input type="file" class="file_one" name="offenderExcelFi ...
- form表单文件上传 servlet文件接收
需要导入jar包 commons-fileupload-1.3.2.jar commons-io-2.5.jar Upload.Jsp代码 <%@ page language="jav ...
随机推荐
- Bootstrap 模态框插件
一.基本使用 使用模态框的弹窗组件需要三层 div 容器元素,分别为 modal(模态声明层). dialog(窗口声明层).content(内容层).在内容层里面,还有三层,分别为 header(头 ...
- 蓝牙HID协议笔记
1.概述 The Human Interface Device (HID)定义了蓝牙在人机接口设备中的协议.特征和使用规程.典型的应用包括蓝牙鼠标.蓝牙键盘.蓝牙游戏手柄等.该协议改编自USB ...
- 为mutable类型的容器(array,set等)添加kvo,有点麻烦,供参考和了解下吧
http://blog.csdn.net/caryaliu/article/details/49284185 需要在被观察的属性所在的类里面实现一些方法,对开发者不友好,一般不建议使用,这里mark一 ...
- HTML5新标签和属性
1.<time>标签(支持IE9以上和其他浏览器) 今年是<time datetime="2015-12-12">2015年</time> &l ...
- ArcGIS Engine开发的ArcGIS 版本管理的功能
原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...
- win7系统中ftp服务器搭建方法(多图)
一.创建FTP站点 1.打开:控制面板---系统和安全---管理工具---Internet 信息服务 2. 建站:右键点击网站---添加FTP站点 3. 输入FTP 站点名称---选择你的 FTP 目 ...
- saltstack之(十)扩展组件salt-ssh
场景:公司有50台服务器,需要安装salt-minion客户端,并完成与salt-master的认证,接受salt-master的管理.本片文章只用了2台服务器做演示,大家可以在roster文件中定义 ...
- 2.TCP_IP互联线缆_TCP_UDP报文抓包详解
TCP_IP互联线缆_TCP_UDP报文抓包详解 2.1网线标准 直通线 交叉线 异种设备互联使用直通线 同种设备互联使用交叉线 TCP和UDP 端口寻址 TCP数据格式 TCP三次握手 UDP数据格 ...
- Android ListView与ExpandableListView设置分割线divider
listview设置分割线需要以下操作: lv.setDivider(getResources().getDrawable(R.drawable.diyline)); ExpandableListVi ...
- Oracle数据库之SQL基础(二)
一.约束 ❤ 1.约束概述 约束作用: (1)定义规则 (2)确保完整性:包括数据的精确性.可靠性.以确保数据不会出错,或者尽量减少出错. 约束的类型: (1)非空约束 (2)主键约束 (3)外键约束 ...