java - uploading of pdf file -
i want upload pdf file using code given below.it give browsing facility dont upload file. when click sendfile button display uploadfile.html code page. how can that??? error in given code???
filename-upload.html
<%@ page language="java" %> <html> <head><title>display file upload form user</title></head> <% // uploading file used encrypt type of multipart/ form-data , input of file type browse , submit file %> <body> <form enctype="multipart/form-data" action= "uploadfile.html" method=post> <br><br><br> <center><table border="2" > <tr><center><td colspan="2"><p align= "center"><b>program uploading file</b><center></td></tr> <tr><td><b>choose file upload:</b> </td> <td><input name="f1" type="file"></td></tr> <tr><td colspan="2"> <p align="right"><input type="submit" value="send file" ></p></td></tr> <table> </center> </form> </body> </html>
filename--uploadfile.html
<%@ page import="java.io.*" %> <% //to content type information jsp request header string contenttype = request.getcontenttype(); //here checking content type not equal null , passed data mulitpart/form-data greater or equal 0 if ((contenttype != null) && (contenttype.indexof("multipart/ form-data") >= 0)) { datainputstream in = new datainputstream(request. getinputstream()); //we taking length of content type data int formdatalength = request.getcontentlength(); byte databytes[] = new byte[formdatalength]; int byteread = 0; int totalbytesread = 0; //this loop converting uploaded file byte code while (totalbytesread < formdatalength) { byteread = in.read(databytes, totalbytesread, formdatalength); totalbytesread += byteread; } string file = new string(databytes); //for saving 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 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 new file same name , writing content in new file fileoutputstream fileout = new fileoutputstream(savefile); fileout.write(databytes, startpos, (endpos - startpos)); fileout.flush(); fileout.close(); %><br><table border="2"><tr><td><b>you have upload file name of:</b> <% out.println(savefile); %></td></tr></table> <% } %>
this roseindia code snippet. first of all, worst learning resource ever. don't use it. teaches bad practices. add site blacklist. in fact, "tutorial" site littered advertisement banners , hopelessly outdated low quality code snippets maintained amateurs primary focus on advertisement income instead of on serious teaching. other examples of such crap "tutorial" sites javabeat, tutorialspoint, journaldev, javatpoint, etc. remarkable common thing sites have originated in india.
apart fact incorrectly used .html
file extension instead of .jsp
(even though presented examples correctly .jsp
extensions), there several major problems code snippet:
- the html using '90s style uppercased tags. discouraged.
- the html using
<font>
,<center>
tags deprecated since 1998. - the business logic mingled presentation logic in single jsp file. java code belongs in java class, not in jsp file.
- the parser relying on
content-length
request header not present per se. if header absent, code breaks. - the parser creating byte array of length. may crash server when content length larger available server memory.
- the parser creating
string
based on byte array using server platform default character encoding instead of 1 specified in multi part header. may malform/corrupt result bytes. - the
datainputstream
wrapper unnecessary, code not taking benefit of it. - etc..
- etc..
it's terrible.
the right way upload file jsp submit form @multipartconfig
annotated servlet class , use request.getpart()
file. can find snippet in answer: how upload files server using jsp/servlet?
the right way learn java ee elaborated in answer: java ee web development, start , skills need?
Comments
Post a Comment