Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
FileUpload.jsp

Java
    <div class="col-lg-4 offset-lg-4">
	<form enctype="multipart/form-data" action="/Kastamandap/Admin/Controller     /uploadFile.jsp" method="post">
   <input name="file"  type="file" multiple>
   <input type="submit" class="btn btn-primary" value="upload">
</form>

uploadfile.jsp
String saveFile = "";

  File ff = null;
  String contentType = request.getContentType();
  if ((contentType != null) && contentType.indexOf("multipart/form-data") >= 0) {
      DataInputStream in = new DataInputStream(request.getInputStream());
      int formDataLength = request.getContentLength();
      byte dataBytes[] = new byte[formDataLength];
      int byteRead = 0;
      int totalBytesRead = 0;
      while (totalBytesRead < formDataLength) {
          byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
          totalBytesRead += byteRead;
      }
      String file = new String(dataBytes);
      saveFile = file.substring(file.indexOf("filename=\"") + 10);
      saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
      saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
      int lastIndex = contentType.lastIndexOf("=");
      String boundry = contentType.substring(lastIndex + 1, contentType.length());
      int pos;
      pos = file.indexOf("filename=\"");
      pos = file.indexOf("\n", pos) + 1;
      pos = file.indexOf("\n", pos) + 1;
      pos = file.indexOf("\n", pos) + 1;
      int boundaryLocation = file.indexOf(boundry, pos) - 4;
      int startPos = ((file.substring(0, pos)).getBytes()).length;
      int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

      ff = new File("D:/programmming/JAVA/eclipse-workspace/Kastamandap/WebContent/Images/" + saveFile);
      FileOutputStream fileOut = new FileOutputStream(ff);
      fileOut.write(dataBytes, startPos, (endPos - startPos));
      fileOut.flush();
      fileOut.close();

  }

it works as excepted
--------------------------------
Same code but showing me the error

	String saveFile3 = "";

File ff3 =  null;
String contentType3 = request.getContentType();
if((contentType3 != null) && contentType3.indexOf("multipart/form-data") >= 0 ) {
	DataInputStream in3 = new DataInputStream(request.getInputStream());
	int formDataLength3 = request.getContentLength();
	byte dataBytes3[] = new byte[formDataLength3];
	int byteRead3 = 0;
	int totalBytesRead3=0;
	while(totalBytesRead3 < formDataLength3){
		byteRead3 = in3.read(dataBytes3,totalBytesRead3,formDataLength3);
		totalBytesRead3 += byteRead3;
	}
	String file3 = new String(dataBytes3);

        //error here
	saveFile3 = file3.substring(file3.indexOf("filename=\"")+10);

		saveFile3 = saveFile3.substring(0, saveFile3.indexOf("\n"));
		saveFile3 = saveFile3.substring(saveFile3.lastIndexOf("\\") + 1, saveFile3.indexOf("\""));
		int lastIndex3 = contentType3.lastIndexOf("=");
		String boundry3 = contentType3.substring(lastIndex3 + 1, contentType3.length());
		int pos3;
		pos3 = file3.indexOf("filename=\"");
		pos3 = file3.indexOf("\n", pos3) + 1;
		pos3 = file3.indexOf("\n", pos3) + 1;
		pos3 = file3.indexOf("\n", pos3) + 1;
		int boundaryLocation3 = file3.indexOf(boundry3, pos3) - 4;
		int startPos3 = ((file3.substring(0, pos3)).getBytes()).length;
		int endPos3 = ((file3.substring(0, boundaryLocation3)).getBytes()).length;
		
		 ff3 = new File("D:/programmming/JAVA/eclipse-workspace/Kastamandap/WebContent/Images/"+saveFile3);
		FileOutputStream fileOut3 = new FileOutputStream(ff3);
		fileOut3.write(dataBytes3, startPos3, (endPos3 - startPos3));
		fileOut3.flush();
		fileOut3.close();


What I have tried:

I have tried but it is showing me the String out of bound exception
Exception

org.apache.jasper.JasperException: An exception occurred processing [/Admin/Controller/uploadFile.jsp] at line [71]

68: 	}
69: 	String file3 = new String(dataBytes3);
70: 	saveFile3 = file3.substring(file3.indexOf("filename=\"")+10);
71: 		saveFile3 = saveFile3.substring(0, saveFile3.indexOf("\n"));
72: 		saveFile3 = saveFile3.substring(saveFile3.lastIndexOf("\\") + 1, saveFile3.indexOf("\""));
73: 		int lastIndex3 = contentType3.lastIndexOf("=");
74: 		String boundry3 = contentType3.substring(lastIndex3 + 1, contentType3.length());
Posted
Comments
Richard MacCutchan 30-Nov-18 12:36pm    
The string does not contain what you think it contains. Use your debugger to find out what actual data is being returned. And add some proper error checking to your code; do not just assume the data will be correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900