struts-上传

简介:
一、创建项目
    项目名称:demoupload
二、添加jar包
    commons-fileupload-1.2.2.jar
    commons-io-2.0.1.jar
    commons-lang3-3.1.jar
    freemarker-2.3.19.jar
    javassist-3.11.0.GA.jar
    ognl-3.0.5.jar
    struts2-core-2.3.4.1.jar
    xwork-core-2.3.4.1.jar
三、在web.xml文件中配置过滤器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.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>
四、在WebRoot目录下创建upload目录用于存放上传文件
    /upload
五、添加核心配置文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录下添加配置文件
        配置文件名称:struts.xml
        配置文件内容:
        <?xml version="1.0" encoding="utf-8" ?>
        <!DOCTYPE struts PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                  "http://struts.apache.org/dtds/struts-2.3.dtd">
         <struts>
             <constant name="struts.devMode" value="true" />
             <!-- 配置全局文件大小 -->
             <constant name="struts.multipart.maxSize" value="21851335"></constant>
         </struts>

六、在WEB-INF下创建pages目录
    /pages
    在pages目录下创建页面
    /pages
        /error.jsp
        /success.jsp
七、创建Action
    1.在src目录下创建包
        cn.jbit.demoupload.web.action
    2.在包下创建Action
        Action名称:UploadAction.java
        Action内容:
        public class UploadAction extends ActionSupport {
            private File upload;//文件对象
            private String uploadContentType;//文件类型
            private String uploadFileName;//文件名称
            public String saveFile(){
                ServletContext servletContext = ServletActionContext.getServletContext();
                String path = servletContext.getRealPath("/upload");
                System.out.println(path+"路径");
                File newFile = new File(path,uploadFileName);
                try {
                    FileUtils.copyFile(upload, newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return SUCCESS;
            }
            public File getUpload() {
                return upload;
            }
            public void setUpload(File upload) {
                this.upload = upload;
            }
            public String getUploadContentType() {
                return uploadContentType;
            }
            public void setUploadContentType(String uploadContentType) {
                this.uploadContentType = uploadContentType;
            }
            public String getUploadFileName() {
                return uploadFileName;
            }
            public void setUploadFileName(String uploadFileName) {
                this.uploadFileName = uploadFileName;
            }
        }
    3.在struts核心配置文件中添加配置
        <package name="default" namespace="/" extends="struts-default">
             <action name="*_uploadAction" class="cn.jbit.demoupload.web.action.UploadAction" method="{1}">
                 <interceptor-ref name="defaultStack">
                     <param name="fileUpload.maximumSize">20480</param>
                     <param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
                     <param name="fileUpload.allowedExtensions">jpg,gif</param>
                 </interceptor-ref>
                 <result name="success">/WEB-INF/pages/success.jsp</result>
                 <result name="input">/WEB-INF/pages/error.jsp</result>
             </action>
         </package>
八、创建页面
    页面名称:index.jsp
    页面内容:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      
      <body>
        <form action="${pageContext.request.contextPath }/saveFile_uploadAction.action" method="post" enctype="multipart/form-data">
            上传文件:
            <input type="file" name="upload"/>
            <input type="submit" value="提交"/>
        </form>
      </body>
    </html>

    本文转自  素颜猪  51CTO博客,原文链接:    


相关文章
|
3天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23294 2
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
5天前
|
人工智能 API 开发工具
Claude Code国内安装:2026最新保姆教程(附cc-switch配置)
Claude Code是我目前最推荐的AI编程工具,没有之一。 它可能不是最简单的,但绝对是上限最高的。一旦跑通安装、接上模型、定好规范,你会发现很多原本需要几小时的工作,现在几分钟就能搞定。 这套方案的核心优势就三个字:可控性。你不用依赖任何不稳定服务,所有组件都在自己手里。模型效果不好?换一个。框架更新了?自己决定升不升。 这才是AI时代开发者该有的姿势——不是被动等喂饭,而是主动搭建自己的生产力基础设施。 希望这篇保姆教程,能帮你顺利上车。做出你自己的作品。
7981 18
Claude Code国内安装:2026最新保姆教程(附cc-switch配置)
|
12天前
|
缓存 人工智能 自然语言处理
我对比了8个Claude API中转站,踩了不少坑,总结给你
本文是个人开发者耗时1周实测的8大Claude中转平台横向评测,聚焦Claude Code真实体验:以加权均价(¥/M token)、内部汇率、缓存支持、模型真实性及稳定性为核心指标。
4744 24
|
8天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
3328 11
|
7天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
2711 9
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
24天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
20391 61
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)