简体   繁体   中英

Send File path from jsp to Java

I want to upload an excel file to a html/jsp page and send the file path to my Java controller. Here is my code snippet:-

The jsp:

<script>
function getPath()
{
    document.getElementById("actualPath").value=document.getElementById("excelFile").value;
    console.log(document.getElementById("actualPath").value);
    return false;
    }


</script>
</head>
<body>

<form action="<%=request.getContextPath()%>/Controller?action=uploadExcel" onsubmit="return getPath()" method="POST">
<input type="file" name="excelFile" id="excelFile">
<input type="hidden" name="actualPath" id="actualPath" value="">
<input type="submit">

</form>

The Java Code:-

private void getExcel(HttpServletRequest request,
                    HttpServletResponse response) throws BiffException, IOException, JSONException, ServletException {



 System.out.println(request.getParameter("excelFile"));
 String FilePath = request.getParameter("actualPath");
 Workbook workbook = Workbook.getWorkbook(new File(FilePath));
 Sheet sh = workbook.getSheet("Sheet1");
 int totalNoOfRows = sh.getRows();
 Map<String,String> map=new LinkedHashMap<String, String>();
 int totalNoOfCols = sh.getColumns();

I get the actualPath value to be C:\\fakepath\\Book1.xls which is obvious since its not allowed to get the file path. Is there any way around by which I could solve this. Or send the file to Java directly?? Thanks in advance

As I do not have enough reputation to post comment I am posting this as answer. The following link will give you details about uploading files to JSP/Servlet.

How to upload files to server using JSP/Servlet?

It shows you 'fakepath' just because of browsers' security rules. I broke my back on trying to find a way to get the full path, but I didn't get lucky. I use Ajax with JSP (only onClick - not input submit) & today I found what I needed in this link . Hope it helps ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM