// Import Servlet Libraries import javax.servlet.*; import javax.servlet.http.*; // Import Java Libraries import java.io.*; import java.util.*; import java.lang.*; public class FileLoad extends HttpServlet { String authorDir ="/export/home/faculty/ofut/public_html/FileLoad/"; /** ********************************************************** * doPost() ************************************************************ */ public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletOutputStream out = null; DataInputStream in = null; FileOutputStream fileOut = null; try { /* set content type of reponse and get handleto output stream in case weare unable to redirect client */ res.setContentType("text/plain"); out = res.getOutputStream(); } catch(IOException e) { //print error message to standard out System.out.println("Error getting output stream"); System.out.println("Error description: " + e); return; } try { /* get content type of client request */ String contentType = req.getContentType(); if(contentType != null && contentType.indexOf("multipart/form-data") != -1) { //open input stream from client to capture upload file in = new DataInputStream(req.getInputStream()); //get length of content data int formDatalength = req.getContentLength(); //check for maximum size violation /* if(formDatalength > MAX_SIZE) { out.println("Sorry, file is too large to upload"); out.flush(); return; } */ //allocate a byte array to store content data byte dataByte[] = new byte[formDatalength]; //read file into byte array int bytesRead = 0; int totalBytesRead = 0; out.println("dataByte length: " + dataByte.length); /* while(totalBytesRead < formDatalength) { bytesRead = in.read(dataByte, totalBytesRead, formDatalength); //keep track of total bytes read from input stream totalBytesRead += bytesRead; out.println("totalBytesRead: " + totalBytesRead); } */ while(totalBytesRead < formDatalength) { dataByte[totalBytesRead] = in.readByte(); //keep track of total bytes read from input stream totalBytesRead ++; } out.println("totalBytesRead: " + totalBytesRead); //create string fom byte array for easy manipulation String file = new String(dataByte); //since byte is store is in string, release memory dataByte = null; //get boundary value int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex+1, contentType.length()); //get filename of upload file String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\"")); out.println("saveFile: " + saveFile); //remove boundary markers and other multipart/form-data tags //from beginning of upload file section int pos; //position in upload file //find position of upload file section of request pos = file.indexOf("filename=\""); //find position of content-disposition line pos = file.indexOf("\n", pos) + 1; //find position of content-type line pos = file.indexOf("\n", pos) + 1; //find position of blank line pos = file.indexOf("\n", pos) + 1; //find location of the next boundary marker //(mark of the end of the upload file) int boundaryLocation =file.indexOf(boundary, pos) - 4; //upload file lies between pos and boundaryLocation file = file.substring(pos, boundaryLocation); //build the full path of the upload file String fileName = new String(authorDir + saveFile); out.println("fileName: " + fileName); //instantiate file output stream fileOut = new FileOutputStream(fileName); //write the string to the file as a byte array fileOut.write(file.getBytes(), 0, file.length()); } else //request is not multipart/form-data { out.println("Request not multipart/form-data."); } } catch(Exception e) { try { System.out.println("Error in doPost: " + e); out.println("An unexpected error has occured."); out.println("Error description: " + e); } catch(Exception f) {} } finally { try { fileOut.close(); } catch(Exception f) {} try { in.close(); } catch(Exception f) {} try { out.close(); } catch(Exception f) {} } } }