如何在上传文件时将输入文本值传递给servlet?

时间:2017-07-12 10:50:12

标签: java jsp servlets

我有两个文件,一个是jsp文件,第二个是servlet文件。

当我在jsp文件中包含输入文本字段以将文本传递给servlet文件然后作为运行提供错误消息的程序的时间时,为什么会发生这种情况?

index.jsp代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>File Upload</title>
    </head>
    <body>
        <center>
            <h1>File Upload</h1>
            <form method="post" action="UploadServlet"
            enctype="multipart/form-data">
            <table border="1">
            <tr><td><h3>Input Text at Top of image :</h3> </td><td><input type="text" name="name1" /></td></tr>
            <tr><td><h3>Input Text at Bottom of image :</h3> </td><td><input type="text" name="name2" /></td></tr>
            </table><br>
            Select file to upload: <input type="file" name="file" size="60" /><br />
            <br /> <input type="submit" value="Upload" />
            </form>
        </center>
    </body>
</html>

UploadServlet.java servlet文件:

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,       // 10MB
maxRequestSize=1024*1024*50)    // 50MB

public class UploadServlet extends HttpServlet 
{               
    /**
     * Name of the directory where uploaded files will be saved, relative to
     * the web application directory.
     */
    private static final String SAVE_DIR = "uploadFiles";

    /**
     * handles file upload
     */
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException 
    {
        // gets absolute path of the web application
        String appPath = request.getServletContext().getRealPath("");
        // constructs path of the directory to save uploaded file
        String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala";

        // creates the save directory if it does not exists
        File fileSaveDir = new File(savePath);
        //File dir = new File("E:/Users/al/tmp/000/111/222");
        if(fileSaveDir.exists()){}
        else
        {// create multiple directories at one time
            boolean successful = fileSaveDir.mkdirs();
            if (successful)
            {
                // created the directories successfully
                System.out.println("directories were created successfully");
            }
            else
            {
                // something failed trying to create the directories
                System.out.println("failed trying to create the directories");
            }
        }

        // create multiple directories at one time
        //boolean successful = fileSaveDir.mkdirs();
        System.out.println(savePath);
        //savePath="C:\\Users\\SANJAY GUPTA\\Downloads\\eclipse";
        for (Part part : request.getParts()) 
        {
            String fileName = extractFileName(part);
            // refines the fileName in case it is an absolute path
            fileName = new File(fileName).getName();
            part.write(savePath + File.separator + fileName);
        }
        String text1 = "";
        String text2 = "";
        text1 = request.getParameter("name1");
        text2 = request.getParameter("name2");
        request.setAttribute("message", "Upload has been done successfully!"+"<br>"+savePath);
                    getServletContext().getRequestDispatcher("/message.jsp").forward(
                            request, response);
    }

    /**
    * Extracts file name from HTTP header content-disposition
    */
    private String extractFileName(Part part) 
    {
        String contentDisp = part.getHeader("content-disposition");
        String[] items = contentDisp.split(";");
        for (String s : items) 
        {
            if (s.trim().startsWith("filename")) 
            {
                return s.substring(s.indexOf("=") + 2, s.length()-1);
            }
        }
        return "";
    }
    //UploadServlet u = new UploadServlet();

}

如何将从jsp传递给servlet的值传递给text1和text2字符串变量? 错误附加为图片error after including the html code as follow :

    <table border="1">
    <tr><td><h3>Input Text at Top of image :</h3> </td><td><input type="text" name="name1" /></td></tr>
    <tr><td><h3>Input Text at Bottom of image :</h3> </td><td><input type="text" name="name2" /></td></tr>
    </table>

错误的文字:

HTTP状态500 - java.io.FileNotFoundException:C:\ Users \ Dell \ workspaceluna1.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ UploadServlet31 \ uploadFiles \ kala(访问被拒绝)

type Exception report

message java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.io.IOException: java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)
    net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    java.io.FileOutputStream.<init>(FileOutputStream.java:171)
    org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:372)
    net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.73 logs.

0 个答案:

没有答案