第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文件上传(单文件)的更多相关文章

  1. Struts2之文件上传(单文件/多文件)

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  2. Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)

    准备工作: 需要先搭建一个spirngmvc的maven项目 1.加入jar包 <dependency> <groupId>commons-fileupload</gro ...

  3. 基于spring的文件上传--单文件上传

    Spring配置文件 <bean id="multipartResolver" class="org.springframework.web.multipart.c ...

  4. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  5. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  6. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

  7. webAPI文件上传时文件过大404错误的问题

    背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...

  8. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  9. 【文件上传】文件上传的form表单提交方式和ajax异步上传方式对比

    一.html 表单代码 …… <input type="file" class="file_one" name="offenderExcelFi ...

  10. form表单文件上传 servlet文件接收

    需要导入jar包 commons-fileupload-1.3.2.jar commons-io-2.5.jar Upload.Jsp代码 <%@ page language="jav ...

随机推荐

  1. C#访问url地址并返回数据

    public partial class Form1 : Form { static bool isSelect = false; public Form1() { InitializeCompone ...

  2. oracle截取某个字符前面的字符串

    已验证. 要求:A.数据库表中的一个字符串 可能含有"+" 例:ORC+001 也可能不含“+” B.要求如果该字符串含有“+”,则取“+”之前的字符 例:ORC+001 取ORC ...

  3. Oracle横向纵向汇总

    Oracle横向纵向汇总 有一张表test 如下, (NO 学生编号 ,cj 成绩) NO name KM CJ 001 张三 语文 80  001 张三 数学 86  001 张三 英语 75  0 ...

  4. HTML标签的改变

    /*这些都是前端面试中经常考到的内容,必须要掌握的*/ 一.新的文档类型声明(DTD) 1.HTML5的DTD声明为:<!doctype html>或者<!DOCTYPE html& ...

  5. django 笔记

    最近开始接触django,一些基本的操作记录于此. 参考链接: http://www.ziqiangxuetang.com/django/django-tutorial.html django安装 s ...

  6. C# 3.0 LINQ的准备工作

    局部变量 隐式类型允许你用var修饰类型.用var修饰只是编译器方便我们进行编码,类型本身仍然是强类型的,所以当编译器无法推断出类型时(例如你初始化一个变量却没有为其赋值,或赋予null,此时就无法推 ...

  7. route命令(转)

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  8. Ubuntu 安装软件

    1,安装pthread的man文档 sudo apt-get install manpages-posix manpages-posix-dev

  9. 堆heap和栈Stack(百科)

    堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...

  10. 在网页中弹出qq临时会话

    http://www.54kefu.net/linshi/201202/391.html