java - struts2中请求的资源不可用 文件上传

标签 java jsp struts2

我是 Struts2 新手,正在开发一个带有文件上传选项的 Web 应用程序。一切都已配置,但是当我运行模块时 HTTP Status 404 错误:请求的资源不可用。 (链接:http://localhost:8080/File_Upload/index.jsp)。请在下面找到我的配置, 索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>File Upload</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.11.3.js"></script>  
        <script type="text/javascript" src="<%=request.getContextPath()%>/js/ajaxfileupload.js"></script>  
        <script type="text/javascript">  
    function ajaxFileUpload()  
    {  

        $("#loading")  
        .ajaxStart(function(){  
            $(this).show();  
        })//  
        .ajaxComplete(function(){  
            $(this).hide();  
        });//  

        $.ajaxFileUpload  
        (  
            {  
                url:'fileUploadAction.action',//  
                secureuri:false,//false  
                fileElementId:'myFile',//id  <input type="file" id="file" name="file" />  
                dataType: 'json',// json  
                success: function (data, status)  //  
                {  
                    //alert(data.message);//jsonmessage,messagestruts2  
                    $("#file_name").attr("value",data.myFile);
                    $("#file_path").attr("value",data.myFileFileName);
                    if(typeof(data.error) != 'undefined')  
                    {  
                        if(data.error != '')  
                        {  
                            alert(data.error);  
                        }else  
                        {  
                            alert(data.myFile);  
                        }  
                    }  
                },  
                error: function (data, status, e)//  
                {  
                    alert(e);  
                }  
            }  
        )  

        return false;  

    }  
    </script>  
</head>
<body>
   <form action="upload" method="post" enctype="multipart/form-data">
      <label for="myFile">Upload your file</label>
      <input type="file" name="myFile" id="myFile"/>
      <input type="button" value="" onclick="return ajaxFileUpload();">
      <input type="hidden" name="file_name[]" id="file_name[]" value=""/>
      <input type="hidden" name="file_path[]" id="file_path[]" value=""/> 
      <input type="submit"/>
   </form>
   <div id="loading"><span style="position: fixed;left: 50%;top: 50%; transform: translate(-50%, -50%); color:#f3cbbb ">
   <img src="<%=request.getContextPath()%>/images/ajax-loader.gif" alt="loading" width="200" height="200" style="padding-left:85px">
   <br /><br />
   <span style=" font-weight: 700; font-size: 18px">Please wait,File is being uploaded
   </span></span>
   </div>

</body>
</html>

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>
   <package name="default" extends="struts-default">
    <action name="fileUploadAction" class="com.tutorialspoint.struts2.MultipleFileUploadAction">  
              <result type="json" name="success">  
                <param name="contentType">text/html</param>  
            </result>  
            <result type="json" name="error">  
                <param name="contentType">text/html</param>  
            </result> 
        </action> 
   </package>
</struts>

Action 类:

package com.tutorialspoint.struts2;
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException; 
import com.opensymphony.xwork2.ActionSupport;
public class MultipleFileUploadAction extends ActionSupport {

    private File[] myFile;
       private String[] myFileContentType;
       private String[] myFileFileName;
       private String destPath;

       public String execute()
       {
          /* Copy file to a safe location */
          destPath = "E:/kalidass/Upload/";

          try{


             for(int i=0;i<myFile.length;i++)
             {
                System.out.println("Src File name: " + myFile[i]);
                 System.out.println("Dst File name: " + myFileFileName[i]);
             File destFile  = new File(destPath, myFileFileName[i]);
             FileUtils.copyFile(myFile[i], destFile);
             }

          }catch(IOException e){
             e.printStackTrace();
             return "error";
          }

          return "success";
       }
       public File[] getMyFile() {
          return myFile;
       }
       public void setMyFile(File[] myFile) {
          this.myFile = myFile;
       }
       public String[] getMyFileContentType() {
          return myFileContentType;
       }
       public void setMyFileContentType(String[] myFileContentType) {
          this.myFileContentType = myFileContentType;
       }
       public String[] getMyFileFileName() {
          return myFileFileName;
       }
       public void setMyFileFileName(String[] myFileFileName) {
          this.myFileFileName = myFileFileName;
       }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app>  
  <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>  
</web-app>  

jar 文件: Jar files

最佳答案

您必须在 default 包中的 struts.xml 中添加此操作

<action name="index">
    <result>/index.jsp</result>
</action>

这样当你尝试打开http://localhost/myapp/index时它将返回 index.jsp

关于java - struts2中请求的资源不可用 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197573/

相关文章:

javascript - 如何设置<c :forEach begin ="1" end ="?"> tag based on Selected DropDown的结束值

java - Struts2 访问网页内容中的文件

java - 从 SD 卡读取和写入文件

c# - C 和 C++ 以外的语言中未定义行为的示例

java - 从 JSP 检索数据

jsp - 如果 scratchdir 是只读的,Tomcat 是否会在内存中缓存已编译的 JSP 页面?

java - 更新主线程不起作用

java - Java 运行时环境 : OpenCV ellipse detection in Java 检测到 fatal error

java - struts2中如何使用jsp将值传递到新窗口

java - Struts 2带有注释错误: Unable to locate parent package