0

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

sss999
  • 528
  • 3
  • 14

2 Answers2

1

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?

Community
  • 1
  • 1
Ashraf Purno
  • 1,065
  • 6
  • 11
0

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 ;)