如何在jsp scriptlet中获取上传的文件 [英] how to get uploaded file within jsp scriptlet

查看:89
本文介绍了如何在jsp scriptlet中获取上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用request.getparameter("filename")及其返回的'null'访问jsp scriptlet中的上载文件.请让我知道如何使用jsp本身在scriptlet中获取文件

I am trying to access a uploaded file within jsp scriptlet using request.getparameter("filename") and its returning 'null'. Please let me know how to get the file in scriptlet using jsp itself

推荐答案

我想您已经知道使用scriptlet被认为是一种罪过. 如何避免JSP文件中的Java代码? 在本演示中,我将使用一个.不建议在Web应用程序中存储上传的文件. 如何使用JSP/Servlet将文件上传到服务器? 我将在此演示中做到这一点. 这是一个文件(放在您的Web应用程序的根文件夹中),我们可以用来上传文件.

I guess you already know that using scriptlets is considered a sin. How to avoid Java code in JSP files? I will use one for this demonstration. Storing uploaded files within the web app is not recommended. How to upload files to server using JSP/Servlet? I will do it in this demonstration. Here is a file(put in your web app's root folder) that we can use to upload a file.

<html>
    <body>
        <form action="uploadJSP" method="post" enctype="multipart/form-data">
            <input type="file" name="myFile" />
            <input type="submit" />
        </form>
    </body> 
</html>

action属性指向web.xml文件中为我们的JSP提供的servlet名称.之所以必须这样做,是因为我们希望Servlet容器处理我们的多部分请求.在我们的网络应用程序的web.xml文件中,应包含

The action attribute points to the servlet name given to our JSP in our web.xml file. We must do this because we want the Servlet container to handle our multipart request. In our web app's web.xml file, we should include something like

<servlet>   
               <servlet-name>uploadfile</servlet-name>
               <jsp-file>/uploadFile.jsp</jsp-file>
               <multipart-config>
                   <location>/temp</location>
                   <max-file-size>20848820</max-file-size>
                   <max-request-size>418018841</max-request-size>
                   <file-size-threshold>1048576</file-size-threshold>
               </multipart-config>
</servlet>
<servlet-mapping>
                <servlet-name>uploadfile</servlet-name>
                <url-pattern>/uploadJSP</url-pattern>
</servlet-mapping>

这里是uploadFile.jsp,它放在您的Web应用程序的根文件夹中.

Here is uploadFile.jsp put it your web app's root folder.

<%@ page import="java.io.*,java.nio.file.*" %>
<%
    Part part = request.getPart("myFile");
    String submittedName = part.getSubmittedFileName();
    String fileName = new File(submittedName).getName();
    String folder = application.getRealPath("/fileUploads");
    Path path = FileSystems.getDefault().getPath(folder, fileName);
    Files.copy(part.getInputStream(), path);
%>
The file <%=fileName%> was uploaded to the <%=folder%> folder.

如果有任何问题,请发布.如果遇到抱怨temp文件夹的错误,请继续在容器要查找的位置创建一个.我的Tomcat希望将它放在工作文件夹中.在Web应用程序的根文件夹中创建一个fileUploads文件夹.

If you have any problems, then post them. If you get an error that complains about the temp folder, just go ahead and create one wherever your container is looking for it. My Tomcat wanted it in it's work folder. Create a fileUploads folder in your web app's root folder.

这篇关于如何在jsp scriptlet中获取上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆