当前位置: 技术问答>java相关
求救:jsp和servlet中如何实现upload文件?
来源: 互联网 发布时间:2014-12-22
本文导语: 如有源码更好,望告知,高分酬谢 | import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.jspsmart.upload.*; public class servletUpload extends HttpServlet { private ServletConfi...
如有源码更好,望告知,高分酬谢
|
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 () {
}
}
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 () {
}
}
|
用smartupload