利用struts2如何实现一个多文件上传功能

这期内容当中小编将会给大家带来有关利用struts2如何实现一个多文件上传功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联建站-专业网站定制、快速模板网站建设、高性价比尼元阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式尼元阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖尼元阳地区。费用合理售后完善,十多年实体公司更值得信赖。

开发环境JDK1.8 eclipse struts2-2.3.31

1.创建web项目

2.导入struts2核心jar包

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

4.创建src/struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  
    
      
      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文件夹,作为上传文件的存储位置

上述就是小编为大家分享的利用struts2如何实现一个多文件上传功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网站题目:利用struts2如何实现一个多文件上传功能
网页网址:http://scyanting.com/article/jioijs.html