这是我在struts应用程序项目中用于上传文件的文件。我在运行代码时获得“状态500”
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name />
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
的struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="CmdForm" type="com.intermarks.struts.CmdForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/upload" type="org.apache.struts.actions.CmdAction">
<forward name="display" path="/upload.jsp" />
</action>
</action-mappings>
</struts-config>
下面是actionform
package com.intermarks.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class CmdForm extends ActionForm {
private FormFile ff;
public FormFile getFf() {
return ff;
}
public void setFf(FormFile ff) {
this.ff = ff;
}
}
以下是动作类
package com.intermarks.struts;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//coder of the year "vinod paul"
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class CmdAction extends Action
{
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CmdForm cf =(CmdForm) form;
FormFile file1 =cf.getFf();
InputStream stream1 = file1.getInputStream();
String fname1 = file1.getFileName();
OutputStream bos1 = new FileOutputStream("C:/Users/Vinod/Documents"+fname1);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while((bytesRead =
stream1.read(buffer, 0,1024)) !=-1)
{
bos1.write(buffer, 0,bytesRead);
bos1.close();
request.setAttribute("fileName1", fname1);
return mapping.findForward("display");
}
return mapping.findForward("success");
}
}
这是应该出现浏览按钮的页面(upload.jsp)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html:form action="/upload.do" enctype="multipart/form-data">
<html:errors />
<br />
Select File : <html:file property="File1" />
<br />
<html:submit value="Upload" />
</html:form>
这是显示页面(Display.jsp)
<%@ page language="java" %>
<b>The File name1</b>:
<%= request.getAttribute("fileName")%>
<br/>
<!-- <b>The File name2</b>: -->
<!-- <%= request.getAttribute("fileName")%> -->
<!-- <br/> -->
<hr/>