下面的方法是在网上抄下来的,为什么我运行后上传图片后就出现
HTTP Status 404 - /up2/fileUpload2
--------------------------------------------------------------------------------
type Status report
message /up2/fileUpload
description The requested resource (/up2/fileUpload) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/5.0.30
这是哪里有问题呢
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
★ 文件上传页面 fileupload.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="doc" label="File"/>
<s:submit/>
<s:form>
<body>
<html>
★ 处理文件上传 FileUploadAction.java
package fileupload;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport implements ServletContextAware {
private static final long serialVersionUID = -5016873153441103539L;
private File doc;
private String fileName;
private String contentType;
private ServletContext context;
public void setDoc(File file) {
this.doc = file;
}
public void setDocFileName(String fileName) {
this.fileName = fileName;
}
public void setDocContentType(String contentType) {
this.contentType = contentType;
}
public void setServletContext(ServletContext context) {
this.context = context;
}
public String execute() throws Exception {
String targetDirectory = context.getRealPath("/upload");
String targetFileName = generateFileName(fileName);
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(doc, target);
return SUCCESS;
}
private String generateFileName(String fileName) {
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
}
★ 配置文件 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="fileupload" namespace="/fileupload" extends="struts-default">
<action name="fileUpload" class="fileupload.FileUploadAction">
<result>/fileupload/upload_success.jsp</result>
</action>
</package>
</struts>
★ 配置文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>