哪位高手知道怎样用jsp 上传文件
本文导语: 我现在要用 jsp写一个文档柜,包括文件上传和文件管理,哪位大虾知道 最好有源代码参考,分大大的有 | 收信 | 源码1: import java.io.*; import java.util.*; import javax.servlet.*; imp...
最好有源代码参考,分大大的有
|
|
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
public class servletUpload extends HttpServlet {
private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
final public ServletConfig getServletConfig() {
return config;
}
/**
* Handles GET requests
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("jspSmartUpload : Servlet Sample");
out.println("
");
out.println("The method of the HTML form must be POST.");
out.println("");
out.println("");
}
/**
* Handles POST requests
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("jspSmartUpload : Servlet Sample");
out.println("");
// Variables
int count=0;
SmartUpload mySmartUpload = new SmartUpload();
try {
// Initialization
mySmartUpload.initialize(config,request,response);
// Upload
mySmartUpload.upload();
// Save the file with the original name
// in a virtual path of the web server
count = mySmartUpload.save(mySmartUpload.getRequest().getParameter("PATH"));
// Display the result
out.println(count + " file uploaded.");
} catch (Exception e){
out.println("Unable to upload the file.
");
out.println("Error : " + e.toString());
}
out.println("");
out.println("");
}
/**
* Destroy the servlet
*/
public void destroy () {
}
}
源码2:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class UploadTest extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Set the content type of the response
resp.setContentType("text/html");
// Get the PrintWriter to write the response
java.io.PrintWriter out = resp.getWriter();
// Create the HTML form
out.println("");
out.println("");
out.println("Send Email");
out.println("Send Email to Karl Moss");
out.println("
");
out.println("");
out.println("Name:");
out.println("");
out.println("File:");
out.println("");
out.println("
");
out.println("");
out.println("");
out.println("");
// Wrap up
out.println("");
out.println("");
out.flush();
}
/**
*
Performs the HTTP POST operation
*
* @param req The request from the client
* @param resp The response from the servlet
*/
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Set the content type of the response
resp.setContentType("text/html");
// Create a PrintWriter to write the response
java.io.PrintWriter out =
new java.io.PrintWriter(resp.getOutputStream());
//指定上传文件最大字节
MultipartRequest multi = new MultipartRequest(req, ".", 2*1024);
//或用默认2M,MultipartRequest multi = new MultipartRequest(req, ".");
out.println("Params:");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()) {
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println(name + " = " + value);
}
out.println();
out.println("Files:");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("f.toString(): " + f.toString());
out.println("f.getName(): " + f.getName());
out.println("f.exists(): " + f.exists());
out.println("f.length(): " + f.length());
out.println();
}
}
// Wrap up
out.flush();
}
/**
*
Initialize the servlet. This is called once when the
* servlet is loaded. It is guaranteed to complete before any
* requests are made to the servlet
*
* @param cfg Servlet configuration information
*/
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
/**
*
Destroy the servlet. This is called once when the servlet
* is unloaded.
*/
public void destroy()
{
super.destroy();
}
}
|
不過兄弟用的也是從www.jspsmart.com下載的一個文件上傳的類吧.
|
另外一个是cos.jar,是从oriely.com中下载的,如果要,我可以给你!