我在网上下了一个JspSmart组件,可不能用,很急。请问哪个大哥用过此组件。请指教一下。多谢先!!!
我在网上下了一个JspSmart组件,可不能用,很急。请问哪个大哥用过此组件。请指教一下。多谢先!!!
在网上找到一篇,一位大侠写的文章,copy出来供选择,
可以实现文件上传,大小,路径都可以自己设置.
//////////////////////////////////
今天在网络上看到一个利用Oreilly的package网页文件上传递(多文件 jsp fileload)的东西,把它修改了一下。整理如下.
运行环境Tomcat 5.0.12,需要下载相应的包到 WEB-INF/lib目录下(copy cos.jar to WEB-INF/lib目录下),然后就可以了。
下载地址 http://www.servlets.com/cos/
我使用的版本 http://www.servlets.com/cos/cos-05Nov2002.zip
1。上传页面
//------------------------------------
<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
<b>文件上传</b></font></p>
<form name="Form1" enctype="multipart/form-data" method="post" action="oreillyfilesaveservlet">
<p>文件上传1: <input type="file"name="File1" size="20" maxlength="20"> </p>
<p>文件上传1描述: <input type="text" name="File1" size="30" maxlength="50"> </p>
<p>文件上传2: <input type="file" name="File2" size="20" maxlength="20"> </p>
<p>文件上传2描述: <input type="text" name="File2" size="30" maxlength="50"> </p>
<p>文件上传3: <input type="file" name="File3" size="20" maxlength="20"> </p>
<p>文件上传3描述: <input type="text" name="File3" size="30" maxlength="50"> </p>
<p> <input type="submit"value="上传"> <input type="reset" value="清除"> </p>
</form>
</body>
</html>
//-------------------------------------------------------------------------------------
2。页面保存servlet
//-------------------------------------------------------------------------------------
package oreilly_fileload_test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.oreilly.servlet.MultipartRequest;
/**
*
Title:
*
* Description:
*
* Copyright: Copyright (c) 2004
*
* Company:
*
* @author wdz123@hotmail.com
* @version 1.0
* @see http://www.javaworld.com.tw/Old/High/Upload.htm 这个例子再重复上传有些问题
* @see http://www.servlets.com/resources/com.oreilly.servlet/ 免費下載。
*/
public class OreillyFileSaveServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String saveDirectory = "C:Upload";
private static final int maxPostSize = 5 * 1024 * 1024;
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("
The servlet has received a " + request.getMethod() +
". This is the reply.
");
out.println("");
out.println("");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
String FileDescription[] = {"", "", ""};
String FileName = null;
int count = 0;
//处理文件保存
MultipartRequest multi = new MultipartRequest(request, saveDirectory,
maxPostSize);
if (multi.getParameter("File1") != null) {
FileDescription[0] = multi.getParameter("File1");
} else {
FileDescription[0] = "";
}
if (multi.getParameter("File2") != null) {
FileDescription[1] = multi.getParameter("File2");
} else {
FileDescription[1] = "";
}
if (multi.getParameter("File3") != null) {
FileDescription[2] = multi.getParameter("File3");
} else {
FileDescription[2] = "";
}
// 获得上传文件的相关信息
Enumeration filesname = multi.getFileNames();
while (filesname.hasMoreElements()) {
String name = (String) filesname.nextElement();
FileName = multi.getFilesystemName(name);
File f = multi.getFile(name);
String ContentType = multi.getContentType(name);
if (FileName != null) {
count++;
out.println(
" 你上传的第个文件:
");
out.println("文件名称为:" + FileName + "
");
out.println("文件类型为:" + ContentType + "
");
out.println("文件的说明:" + FileDescription[count - 1] + "
");
}
}
out.println("返回上传页面");
out.println("");
out.println("");
out.close();
}
}
//---------------------------end OreillyFileSaveServlet----------------------------------------------------------
3.web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>WebModule1</display-name>
<servlet>
<servlet-name>oreillyfilesaveservlet</servlet-name>
<servlet-class>oreilly_fileload_test.OreillyFileSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>oreillyfilesaveservlet</servlet-name>
<url-pattern>/oreillyfilesaveservlet</url-pattern>
</servlet-mapping>
</web-app>
//-------------------------end web.xml文件------------------------------------------------------------
对不起,前几天网络问题,邮件传不出去,现在不知道下载到没有?
呵呵,给你传去了.
出现一个问题,还想请教一下,我把com文件夹打包为com.jar放到的jdk/jre/ext下了,Servlet编译成功,Servlet配置也应该不会有问题,可上传时去不成功,请教一下怎么办?多谢!!!