How to upload files using JavaServer Pages

  • Published on
    15-Oct-2014

  • View
    3.606

  • Download
    0

Transcript

How to upload files using JavaServer Pages Uploading files to a web server in JavaServer pages proves to be sometimes technical. This is why I came up with these simple JavaServer pages in order to achieve this. There is a Windows and a Linux version, though the procedures involved are more or less the same. The Windows version was tested on a Microsoft Windows 2000 Professional system whereas the Linux companion was done on Red Hat 9.0. The web server of service was Apache Tomcat 5.5 in both cases. This is the HTML upload page Code: File Upload in JSP Which file do you want to upload? This is the JavaServer page, Linux version Code: = 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; } try { 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 boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos;} catch(Exception e) { out.print(e); } finally { try{fileOut.close();}catch(Exception err){} } } %>pos = file.indexOf("filename=/" + 1); 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; String folder = "/tmp/uploads/"; fileOut = new FileOutputStream(folder + saveFile); fileOut.write(dataBytes); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush();This is the JavaServer page, Windows version Code: = 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 boundary = 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(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; String folder = "C:/Program Files/Apache Software Foundation/Tomcat5.5/webapps/upload/images/"; FileOutputStream fileOut = new FileOutputStream(folder + saveFile); //out.print("Saved here: " + saveFile); //fileOut.write(dataBytes); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); out.println("Uploading your file. Please wait..."); } %>Please note that this is not a final thing and is subject to revision. Feel free to do so. Thank you

Recommended

View more >