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 ...
随机推荐
- 国外it网站收集
1.http://news.com.com/2.http://www.zdnet.com/3.http://www.salon.com/tech/index.html4.http://www.brin ...
- SQLAlchemy 一对多
下述範例描述了電影同導演的多對一關係.範例中說明了從用戶定義的Python類建立數據表的方法,雙方關係例項的建立方法,以及最終查詢數據的方法:包括延遲載入和預先載入兩種自動生成的SQL查詢. 結構定義 ...
- CFBundleVersion与CFBundleShortVersionString
CFBundleVersion,标识(发布或未发布)的内部版本号.这是一个单调增加的字符串,包括一个或多个时期分隔的整数. CFBundleShortVersionString 标识应用程序的发布版 ...
- 微信小店分类ID列表
一级分类 { , "errmsg": "ok", "cate_list": [ { ", "name": &q ...
- Ubuntu 安装 Brother MFC7470D 驱动
Ubuntu 安装 Brother MFC7470D 驱动 办公室的打印机是 Brother MFC7470D ,在 Ubuntu 中安装打印机驱动时,发现没有这个型号的驱动.只有 MFC7450 的 ...
- PHP接口类interface的正确使用方法
对于那些初学PHP语言的人来说,对于PHP的接口类也许了解的还不是很深入,接下来我们就来具体讲述PHP接口类interface的使用方法. 如何正确运用PHP XMLReader解析XML文档 深入解 ...
- Speed-BI 多事实表与表间计算的应用:销售目标达成分析 另一种实现方法
在前一篇<Speed-BI多事实表与表间计算的应用(excel多Sheet关联分析):销售目标达成分析>http://www.powerbibbs.com/forum. ... 7583& ...
- Power-BI仪表盘文本框排行分析设计要点
例如:我在BI软件中做一个商品类别TOP5排行. 文本框默认绑定第一列,但是没显示是那个品类. 这里我们需要做几个操作来进行优化. 1.把品类显示出来. 在序列中找到标题进行显示,如果位置有重叠可以进 ...
- Java8 新特性default
在JDK1.8的Iterator接口中 package java.util; import java.util.function.Consumer; public interface Iterator ...
- ios理解 -- Pro Mutlithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks
Capturing automatic variables Next, you need to learn what the “together with automatic (local) vari ...