How To Upload Files Using Javaserver Pages

  • August 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View How To Upload Files Using Javaserver Pages as PDF for free.

More details

  • Words: 494
  • Pages: 3
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: <%@ page import="java.io.*" %> <% String contentType = request.getContentType(); String file = ""; String saveFile = ""; FileOutputStream fileOut = null; 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; } 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;

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

} catch(Exception e) { out.print(e); } finally { try{fileOut.close();}catch(Exception err){} } } %>

This is the JavaServer page, Windows version Code: <%@ page import="java.io.*" %> <% String saveFile = ""; 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 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/Tomcat

5.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

Related Documents