栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

struts2实现多文件上传的示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts2实现多文件上传的示例代码

开发环境JDK1.8 eclipse struts2-2.3.31

1.创建web项目

2.导入struts2核心jar包

3.更改web.xml配置文件(只要配置好struts2的Filter就好)

4.创建src/struts.xml文件





  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  
    
      
      WEB-INF/images
      /success.jsp
      /error.jsp
      
 
 image/bmp,image/png,image/gif,image/jpeg
 
 1025956
      
      
      
    
  


5.创建src/com.ifan.action.FileUpload.Java

package com.ifan.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport{

  private File[] image; //上传的文件
  private String[] imageFileName; //文件名称
  private String[] imageContentType; //文件类型

  public String execute() throws Exception {
    ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
    String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    System.out.println(realpath);
    if (image != null) {
      File savedir=new File(realpath);
      if(!savedir.getParentFile().exists())
 savedir.getParentFile().mkdirs();
      for(int i=0;i

6.创建WebContent/index.jsp ,作为上传文件的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
      + path + "/";
%>






My JSP 'hello.jsp' starting page























7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/147271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号