如何使用JSP将文件的位置传递给Java代码? [英] How to pass the location of file to Java code using JSP?
问题描述
以下是JSP代码.
如何修改代码,以便必须将使用浏览"选项选择的文件的位置传递给名为new.java的Java程序
请告知.
<form action="abc.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
Upload File:
<input name="file" type="file" id="file"><br><br>
<input type="submit" name="Submit" value="Submit"/><br><br>
<input type="reset" name="Reset" value="Reset"/>
</form>
上面的html代码仅显示文件名,而不显示位置.而且我什至不知道如何将文件名传递给Java代码本身.
在您的action
中,您必须指定一个用于处理多部分请求的servlet/jsp.最好是一个Servlet(JSP用于表示而不是处理).
因此,我们有一个带有doPost(request, response)
方法的Servlet(public class NewServlet extends HttpServlet
).此时,您可以使用 commons-fileupload 处理传入的文件. 此处是用户指南,其中包含很多代码,仅供复制粘贴. >
Servlet教程是一个不错的起点
The following is the JSP code.
How the code can be modified such that the location of the file that is selected using "Browse" option must be passed to a java program named new.java
Please advise.
<form action="abc.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
Upload File:
<input name="file" type="file" id="file"><br><br>
<input type="submit" name="Submit" value="Submit"/><br><br>
<input type="reset" name="Reset" value="Reset"/>
</form>
The html code above shows only the name of the file and not the location. And I dont even know how to pass the name of the file to java code itself.
In your action
you must specify a servlet/jsp that handles the multipart request. It better be a Servlet (JSP is meant for presentation, not processing).
So, we have a Servlet (public class NewServlet extends HttpServlet
), with a doPost(request, response)
method. At that point you can use commons-fileupload to handle the incoming file. Here is the user guide with a lot of code to just copy-paste.
The servlet tutorial is a good place to start.
这篇关于如何使用JSP将文件的位置传递给Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!