首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jspSmartUpload实现文件上传 代码没有提示异常但是运行时会出现404异常 The requested resource is not avai

2012-03-23 
jspSmartUpload实现文件上传 代码没有提示错误但是运行时会出现404错误The requested resourceis not avai

jspSmartUpload实现文件上传 代码没有提示错误但是运行时会出现404错误 The requested resource is not avai
前台jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jspSmartUpload组件文件上传</title>
</head>

<body>
<font size="+5" color="#009933"><b>jspSmartUpload组件文件上传</b></font><br />
<form name="uploadForm" enctype="multipart/form-data" method="post" action="SmartUploadServlet">
<table border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#FFFAFA">
<input type="hidden" name="paramete" value="OK" />
<tr>
<td>
<div align="center">上传文件1</div>
</td>
<td><div align="center">
<input type="file" name="file1" size="25" maxlength="80" />
</div></td></tr>

<tr>
<td>
<div align="center">上传文件2</div>
</td>
<td><div align="center">
<input type="file" name="file2" size="25" maxlength="80" />
</div></td></tr>
<tr width="100%">
<td>
<div align="center"><input type="submit" value="上传文件">
<input type="reset" value="清除"></div></td></tr>
</table> 
</form>
</body>
</html>

后台的servlet


package lee;



import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import javax.servlet.*;
import com.jspsmart.upload.*;
import java.io.*;


public class SmartUploadServlet extends HttpServlet {
private ServletConfig config;
//初始化Serblet
final public void init(ServletConfig config)throws ServletException{
this.config=config;
}
//处理get请求
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
//获取PrintWriter对象
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body bgcolor='write'>");
out.println("<br><h1>jspSmartUpload</h1>");
//新建一个smartupload对象
SmartUpload mySmartUpload=new SmartUpload();
try{
//上传初始化
mySmartUpload.initialize(config, request, response);
//设定每个文件上传的最大长度
mySmartUpload.setMaxFileSize(1*512*1024);
//设定总上传数据的长度
mySmartUpload.setTotalMaxFileSize(1*1024*1024);
//设定语序上传文件的类型,金鱼徐java文件
mySmartUpload.setAllowedFilesList("java");
//设定精致上传的问价类型,进制伤上传带有exe,bat的文件
mySmartUpload.setDeniedFilesList("exe,bat");
//将上传文件全部保存到指定目录
int count=mySmartUpload.save("/");
//利用request对象,获取参数之值
out.println(mySmartUpload.getRequest().getParameter("paramete")+"nbsp"


);
//显示处理结果
out.println("<font color=red>"+count+"</font>&nbsp;FileUpload!<br>");
//处理每一个上传文件
for(int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File file=mySmartUpload.getFiles().getFile(i);
//判断用户是否选择了文件
if(!file.isMissing()){
out.println("File Name:&nbsp;<font color=red>"+file.getFieldName()+"</font><br>");
//打印扩展名
out.println("file extname:&nbsp;<font color=red>"+file.getFileExt()+"</font><br>");
//打印路径名
out.println("file extname:&nbsp;<font color=red>"+file.getFilePathName()+"</font><br>");
//另存文件
file.saveAs("/upload."+file.getFieldName());
file.saveAs("c:\\"+file.getFieldName(), mySmartUpload.SAVE_PHYSICAL);


}

}


}catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("please cheack the file type");

}

out.println("</center></body>");
out.println("/html");
}


}


配置的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>SmartUploadServlet</servlet-name>
  <servlet-class>SmartUploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>SmartUploadServlet</servlet-name>
  <url-pattern>/lee/SmartUploadServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  <welcome-file>upload.jsp</welcome-file>
  </welcome-file-list>
</web-app>


[解决办法]
action="SmartUploadServlet"
<url-pattern>/lee/SmartUploadServlet</url-pattern>
servlet路徑寫錯了

热点排行