| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 497 人关注过本帖
标题:struts2 文件上传问题????
只看楼主 加入收藏
fcnh1983
Rank: 1
等 级:新手上路
帖 子:665
专家分:0
注 册:2005-8-31
结帖率:100%
收藏
 问题点数:0 回复次数:0 
struts2 文件上传问题????

下面的方法是在网上抄下来的,为什么我运行后上传图片后就出现
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>

搜索更多相关主题的帖子: 文件 
2007-06-27 08:29
快速回复:struts2 文件上传问题????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016642 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved