Note that there are some explanatory texts on larger screens.

plurals
  1. POuploading of pdf file
    primarykey
    data
    text
    <p>i want to upload a pdf file by using code given below.It give browsing facility but dont upload file. When i click sendfile button Its display uploadfile.html code page. How can i do that??? where is the error in the given code???</p> <p>filename-upload.html</p> <pre><code>&lt;%@ page language="java" %&gt; &lt;HTml&gt; &lt;HEAD&gt;&lt;TITLE&gt;Display file upload form to the user&lt;/TITLE&gt;&lt;/HEAD&gt; &lt;% // for uploading the file we used Encrypt type of multipart/ form-data and input of file type to browse and submit the file %&gt; &lt;BODY&gt; &lt;FORM ENCTYPE="multipart/form-data" ACTION= "uploadfile.html" METHOD=POST&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;center&gt;&lt;table border="2" &gt; &lt;tr&gt;&lt;center&gt;&lt;td colspan="2"&gt;&lt;p align= "center"&gt;&lt;B&gt;PROGRAM FOR UPLOADING THE FILE&lt;/B&gt;&lt;center&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;b&gt;Choose the file To Upload:&lt;/b&gt; &lt;/td&gt; &lt;td&gt;&lt;INPUT NAME="F1" TYPE="file"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td colspan="2"&gt; &lt;p align="right"&gt;&lt;INPUT TYPE="submit" VALUE="Send File" &gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;table&gt; &lt;/center&gt; &lt;/FORM&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre> <p>filename--uploadfile.html</p> <pre><code>&lt;%@ page import="java.io.*" %&gt; &lt;% //to get the content type information from JSP Request Header String contentType = request.getContentType(); //here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0 if ((contentType != null) &amp;&amp; (contentType.indexOf("multipart/ form-data") &gt;= 0)) { DataInputStream in = new DataInputStream(request. getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead &lt; formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String 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 boundary = contentType.substring(lastIndex + 1, contentType.length()); int pos; //extracting the index of file 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(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length; // creating a new file with the same name and writing the content in new file FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %&gt;&lt;Br&gt;&lt;table border="2"&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;You have successfully upload the file by the name of:&lt;/b&gt; &lt;% out.println(saveFile); %&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; &lt;% } %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload