使用jsp和servlet从本地驱动器获取文件路径

时间:2015-08-04 21:13:57

标签: java jsp servlets

我想要获取文件路径时遇到问题。这是我的代码:

public void service(HttpServletRequest request, HttpServletResponse res)
        throws ServletException, IOException {
    String cabArch = req.getParameter("fRutaArch");
    String rutaArch = getFileName(filePart);
}

在jsp中我有这个:

<td align="left" class="e2">
  <input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar archivo">                       
</td>
<td>
  <button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>

我只需要完整的文件路径,请提出建议吗?

1 个答案:

答案 0 :(得分:0)

您可以在html代码中添加a hidden field并修改service功能

代码如下

<td align="left" class="e2">
<input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar   archivo" onchange="document.getElementById('filepath').value=this.value" >                       
</td>
<td>
<button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>
<input type='hidden' name='filepath' id='filepath'/>

然后是函数service

public void service(HttpServletRequest request, HttpServletResponse res)
    throws ServletException, IOException {
String cabArch = req.getParameter("filepath");
String rutaArch = getFileName(cabArch);

}