jsp的文件怎么上传

文件上传:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



有关jsp的文件上传,我们需要导一个叫jspsmartupload.jar的包。

例 1.1.1

upload.jsp:

<%@ page contentType="text/html; charset=GBK" %>
<html>
<body>
<h1>
<%
if(request.getParameter("i")!=null)
{
out.print("上传了"+request.getParameter("i")+"个文件");
}
%>
</h1>
we must use post, otheriwse, report negativeArray error.
<form method="post" action="ServletHello2" enctype="multipart/form-data">
<br>
<input  type="text" name="user" value="可以中文"/>
<!--the following name does not matter.-->
<input  type="file" name="upload"/>
<input  type="file" name="upload1"/>
<br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>






package com;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.jspsmart.upload.Files;
import java.io.IOException;
import com.jspsmart.upload.File;
public class ServletHello2 extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /* the following statement can not work, it must be put in the last
         * position. */
        System.out.println("user1 is " + request.getParameter("user"));
        ServletContext sec = this.getServletContext();
        SmartUpload su = new SmartUpload();
        int i = 0;
        String s = sec.getRealPath("");
        System.out.println("s is" + s);
        su.initialize(this.getServletConfig(), request, response);
        try {
            su.upload();
            i = su.save(s + "/upload");
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        Files files = su.getFiles();
        File f = (File) files.getFile(0);
        System.out.println("upload file name is " + f.getFileName());
        com.jspsmart.upload.Request r = su.getRequest();
        System.out.println("中文user is " + r.getParameter("user"));
        response.sendRedirect("upload.jsp?i=" + ("" + i));
    }
}




运行jsp后,上传以后,console里的输出结果是:


user1 is null
s isD:\eclipseJee\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletHello
upload file name is jspsmartupload.jar
中文user is 可以中文