好了,废话少说。
先把Apache Commons FileUpload下载下来。这是一个很简易的文件上传开源包。我们只看关键步骤:
文件上传到服务器文件夹:
package com.XXXXXX.file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.
*
;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

public
class
UploadToFolder extends HttpServlet
...
{


/**//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{

doPost(request,response);
}

/**//**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart)...{
System.out.println("The request is not a file upload request!");
本文介绍了如何使用Apache Commons FileUpload实现文件上传。既可以将文件存储到服务器文件夹,也可以以Blob形式存入数据库。详细阐述了两种方案的关键步骤。
最低0.47元/天 解锁文章
1170

被折叠的 条评论
为什么被折叠?



