如何使用JSP将文件的位置传递给Java代码? [英] How to pass the location of file to Java code using JSP?

查看:55
本文介绍了如何使用JSP将文件的位置传递给Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是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屋!

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