file transfer in servlet programming.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shyaminf
    New Member
    • Feb 2008
    • 5

    file transfer in servlet programming.

    hi everybody!
    iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to execute it using tomcat server5.5.
    the code is as follows.

    if you have any other coding regarding this, please send me.it's urgent.

    import java.io.*;
    import java.util.*;
    import javax.servlet.* ;
    import javax.servlet.h ttp.*;
    //import grid.*;
    public class UploadTest extends HttpServlet {
    public void doPost(HttpServ letRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException {
    res.setContentT ype("text/html");
    PrintWriter out = res.getWriter() ;
    try {
    // Blindly take it on faith this is a multipart/form-data request
    // Construct a MultipartReques t to help read the information.
    // Pass in the request, a directory to save files to, and the
    // maximum POST size we should attempt to handle.
    // Here we (rudely) write to the server root and impose 5 Meg limit.
    MultipartReques t multi = new MultipartReques t(req, ".", 5 * 1024 * 1024);
    out.println("<H TML>");
    out.println("<H EAD><TITLE>Uplo adTest</TITLE></HEAD>");
    out.println("<B ODY>");
    out.println("<H 1>UploadTest</H1>");
    // Print the parameters we received
    out.println("<H 3>Params:</H3>");
    out.println("<P RE>");
    Enumeration params = multi.getParame terNames();
    while (params.hasMore Elements()) {
    String name = (String)params. nextElement();
    String value = multi.getParame ter(name);
    out.println(nam e + " = " + value);
    }
    out.println("</PRE>");
    // Show which files we received
    out.println("<H 3>Files:</H3>");
    out.println("<P RE>");
    Enumeration files = multi.getFileNa mes();
    while (files.hasMoreE lements()) {
    String name = (String)files.n extElement();
    String filename = multi.getFilesy stemName(name);
    String type = multi.getConten tType(name);
    File f = multi.getFile(n ame);
    out.println("na me: " + name);
    out.println("fi lename: " + filename);
    out.println("ty pe: " + type);
    if (f != null) {
    out.println("le ngth: " + f.length());
    out.println();
    }
    out.println("</PRE>");
    }
    }
    catch (Exception e) {
    out.println("<P RE>");
    e.printStackTra ce(out);
    out.println("</PRE>");
    }
    out.println("</BODY></HTML>");
    }
    }





    class MultipartReques t {
    private static final int DEFAULT_MAX_POS T_SIZE = 1024 * 1024;
    private ServletRequest req;
    private File dir;
    private int maxSize;
    private Hashtable parameters = new Hashtable(); // name - value
    private Hashtable files = new Hashtable(); // name - UploadedFile



    public MultipartReques t(ServletReques t request,String saveDirectory) throws IOException {
    //this(request, saveDirectory, DEFAULT_MAX_POS T_SIZE);

    }



    public MultipartReques t(ServletReques t request,String saveDirectory,i nt maxPostSize) throws IOException {
    // Sanity check values
    if (request == null)
    throw new IllegalArgument Exception("requ est cannot be null");
    if (saveDirectory == null)
    throw new IllegalArgument Exception("save Directory cannot be null");
    if (maxPostSize <= 0) {
    throw new IllegalArgument Exception("maxP ostSize must be positive");
    }
    // Save the request, dir, and max size
    req = request;
    dir = new File(saveDirect ory);
    maxSize = maxPostSize;
    // Check saveDirectory is truly a directory
    if (!dir.isDirecto ry())
    throw new IllegalArgument Exception("Not a directory: " + saveDirectory);
    // Check saveDirectory is writable
    if (!dir.canWrite( ))
    throw new IllegalArgument Exception("Not writable: " + saveDirectory);
    // Now parse the request saving data to "parameters " and "files";
    // write the file contents to the saveDirectory

    readRequest();
    }



    public Enumeration getParameterNam es() {
    return parameters.keys ();
    }



    public Enumeration getFileNames() {
    return files.keys();
    }



    public String getParameter(St ring name) {
    try {
    String param = (String)paramet ers.get(name);
    if (param.equals(" ")) return null;
    return param;
    }
    catch (Exception e) {
    return null;
    }
    }



    public String getFilesystemNa me(String name) {
    try {
    UploadedFile file = (UploadedFile)f iles.get(name);
    return file.getFilesys temName(); // may be null
    }
    catch (Exception e) {
    return null;
    }
    }



    public String getContentType( String name) {
    try {
    UploadedFile file = (UploadedFile)f iles.get(name);
    return file.getContent Type(); // may be null
    }
    catch (Exception e) {
    return null;
    }
    }



    public File getFile(String name) {
    try {
    UploadedFile file = (UploadedFile)f iles.get(name);
    return file.getFile(); // may be null
    }
    catch (Exception e) {
    return null;
    }
    }



    protected void readRequest() throws IOException {
    // Check the content type to make sure it's "multipart/form-data"
    String type = req.getContentT ype();
    if (type == null ||
    !type.toLowerCa se().startsWith ("multipart/form-data")) {
    throw new IOException("Po sted content type isn't multipart/form-data");
    }
    // Check the content length to prevent denial of service attacks
    int length = req.getContentL ength();
    if (length > maxSize) {
    throw new IOException("Po sted content length of " + length +
    " exceeds limit of " + maxSize);
    }
    // Get the boundary string; it's included in the content type.
    // Should look something like "------------------------12012133613061"
    String boundary = extractBoundary (type);
    if (boundary == null) {
    throw new IOException("Se paration boundary was not specified");
    }
    // Construct the special input stream we'll read from
    MultipartInputS treamHandler in =new MultipartInputS treamHandler(re q.getInputStrea m(), boundary, length);
    // Read the first line, should be the first boundary
    String line = in.readLine();
    if (line == null) {
    throw new IOException("Co rrupt form data: premature ending");
    }
    // Verify that the line is the boundary
    if (!line.startsWi th(boundary)) {
    throw new IOException("Co rrupt form data: no leading boundary");
    }
    // Now that we're just beyond the first boundary, loop over each part
    boolean done = false;
    while (!done) {
    done = readNextPart(in , boundary);
    }
    }



    protected boolean readNextPart(Mu ltipartInputStr eamHandler in,
    String boundary) throws IOException {
    // Read the first line, should look like this:
    // content-disposition: form-data; name="field1"; filename="file1 .txt"
    String line = in.readLine();
    if (line == null) {
    // No parts left, we're done
    return true;
    }
    // Parse the content-disposition line
    String[] dispInfo = extractDisposit ionInfo(line);
    String disposition = dispInfo[0];
    String name = dispInfo[1];
    String filename = dispInfo[2];
    // Now onto the next line. This will either be empty
    // or contain a Content-Type and then an empty line.
    line = in.readLine();
    if (line == null) {
    // No parts left, we're done
    return true;
    }
    // Get the content type, or null if none specified
    String contentType = extractContentT ype(line);
    if (contentType != null) {
    // Eat the empty line
    line = in.readLine();
    if (line == null || line.length() > 0) { // line should be empty
    throw new
    IOException("Ma lformed line after content type: " + line);
    }
    }
    else {
    // Assume a default content type

    contentType = "applicatio n/octet-stream";
    }
    // Now, finally, we read the content (end after reading the boundary)
    if (filename == null) {
    // This is a parameter
    String value = readParameter(i n, boundary);
    parameters.put( name, value);
    }
    else {
    // This is a file
    readAndSaveFile (in, boundary, filename);
    if (filename.equal s("unknown")) {
    files.put(name, new UploadedFile(nu ll, null, null));
    }
    else {
    files.put(name,
    new UploadedFile(di r.toString(), filename, contentType));
    }
    }
    return false; // there's more to read
    }



    protected String readParameter(M ultipartInputSt reamHandler in,
    String boundary) throws IOException {
    StringBuffer sbuf = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
    if (line.startsWit h(boundary)) break;
    sbuf.append(lin e + "\r\n"); // add the \r\n in case there are many lines
    }
    if (sbuf.length() == 0) {
    return null; // nothing read
    }
    sbuf.setLength( sbuf.length() - 2); // cut off the last line's \r\n
    return sbuf.toString() ; // no URL decoding needed
    }



    protected void readAndSaveFile (MultipartInput StreamHandler in,
    String boundary,
    String filename) throws IOException {
    File f = new File(dir + File.separator + filename);
    FileOutputStrea m fos = new FileOutputStrea m(f);
    BufferedOutputS tream out = new BufferedOutputS tream(fos, 8 * 1024); // 8K
    byte[] bbuf = new byte[8 * 1024]; // 8K
    int result;
    String line;
    // ServletInputStr eam.readLine() has the annoying habit of
    // adding a \r\n to the end of the last line.
    // Since we want a byte-for-byte transfer, we have to cut those chars.
    boolean rnflag = false;
    while ((result = in.readLine(bbu f, 0, bbuf.length)) != -1) {
    // Check for boundary
    if (result > 2 && bbuf[0] == '-' && bbuf[1] == '-') { // quick pre-check
    line = new String(bbuf, 0, result, "ISO-8859-1");
    if (line.startsWit h(boundary)) break;
    }
    // Are we supposed to write \r\n for the last iteration?
    if (rnflag) {
    out.write('\r') ; out.write('\n') ;
    rnflag = false;
    }
    // Write the buffer, postpone any ending \r\n
    if (result >= 2 &&
    bbuf[result - 2] == '\r' &&
    bbuf[result - 1] == '\n') {
    out.write(bbuf, 0, result - 2); // skip the last 2 chars
    rnflag = true; // make a note to write them on the next iteration
    }
    else {
    out.write(bbuf, 0, result);
    }
    }
    out.flush();
    out.close();
    fos.close();
    }



    private String extractBoundary (String line) {
    int index = line.indexOf("b oundary=");
    if (index == -1) {
    return null;
    }
    String boundary = line.substring( index + 9); // 9 for "boundary="
    // The real boundary is always preceded by an extra "--"
    boundary = "--" + boundary;
    return boundary;
    }



    private String[] extractDisposit ionInfo(String line) throws IOException {
    // Return the line's data as an array: disposition, name, filename
    String[] retval = new String[3];
    // Convert the line to a lowercase string without the ending \r\n
    // Keep the original line for error messages and for variable names.
    String origline = line;
    line = origline.toLowe rCase();
    // Get the content disposition, should be "form-data"
    int start = line.indexOf("c ontent-disposition: ");
    int end = line.indexOf("; ");
    if (start == -1 || end == -1) {
    throw new IOException("Co ntent disposition corrupt: " + origline);
    }
    String disposition = line.substring( start + 21, end);
    if (!disposition.e quals("form-data")) {
    throw new IOException("In valid content disposition: " + disposition);
    }
    // Get the field name
    start = line.indexOf("n ame=\"", end); // start at last semicolon
    end = line.indexOf("\ "", start + 7); // skip name=\"
    if (start == -1 || end == -1) {
    throw new IOException("Co ntent disposition corrupt: " + origline);
    }
    String name = origline.substr ing(start + 6, end);
    // Get the filename, if given
    String filename = null;
    start = line.indexOf("f ilename=\"", end + 2); // start after name
    end = line.indexOf("\ "", start + 10); // skip filename=\"
    if (start != -1 && end != -1) { // note the !=
    filename = origline.substr ing(start + 10, end);
    // The filename may contain a full path. Cut to just the filename.
    int slash =
    Math.max(filena me.lastIndexOf( '/'), filename.lastIn dexOf('\\'));
    if (slash > -1) {
    filename = filename.substr ing(slash + 1); // past last slash
    }
    if (filename.equal s("")) filename = "unknown"; // sanity check
    }
    // Return a String array: disposition, name, filename
    retval[0] = disposition;
    retval[1] = name;
    retval[2] = filename;
    return retval;
    }



    private String extractContentT ype(String line) throws IOException {
    String contentType = null;
    // Convert the line to a lowercase string
    String origline = line;
    line = origline.toLowe rCase();
    // Get the content type, if any
    if (line.startsWit h("content-type")) {
    int start = line.indexOf(" ");
    if (start == -1) {
    throw new IOException("Co ntent type corrupt: " + origline);
    }
    contentType = line.substring( start + 1);
    }
    else if (line.length() != 0) { // no content type, so should be empty
    throw new IOException("Ma lformed line after disposition: " + origline);
    }
    return contentType;
    }
    }



    // A class to hold information about an uploaded file.
    //
    class UploadedFile {
    private String dir;
    private String filename;
    private String type;
    UploadedFile(St ring dir, String filename, String type) {
    this.dir = dir;
    this.filename = filename;
    this.type = type;
    }



    public String getContentType( ) {
    return type;
    }



    public String getFilesystemNa me() {
    return filename;
    }



    public File getFile() {
    if (dir == null || filename == null) {
    return null;
    }
    else {
    return new File(dir + File.separator + filename);
    }
    }
    }



    // A class to aid in reading multipart/form-data from a ServletInputStr eam.
    // It keeps track of how many bytes have been read and detects when the
    // Content-Length limit has been reached. This is necessary because some
    // servlet engines are slow to notice the end of stream.
    //
    class MultipartInputS treamHandler {
    ServletInputStr eam in;
    String boundary;
    int totalExpected;
    int totalRead = 0;
    byte[] buf = new byte[8 * 1024];
    public MultipartInputS treamHandler(Se rvletInputStrea m in,String boundary,int totalExpected) {
    this.in = in;
    this.boundary = boundary;
    this.totalExpec ted = totalExpected;
    }



    public String readLine() throws IOException {
    StringBuffer sbuf = new StringBuffer();
    int result;
    String line;
    do {
    result = this.readLine(b uf, 0, buf.length); // this.readLine() does +=
    if (result != -1) {
    sbuf.append(new String(buf, 0, result, "ISO-8859-1"));
    }
    } while (result == buf.length); // loop only if the buffer was filled
    if (sbuf.length() == 0) {
    return null; // nothing read, must be at the end of stream
    }sbuf.setLength (sbuf.length() - 2); // cut off the trailing \r\n
    return sbuf.toString() ;
    }



    public int readLine(byte b[], int off, int len) throws IOException {
    if (totalRead >= totalExpected) {
    return -1;
    }
    else {
    int result = in.readLine(b, off, len);
    if (result > 0) {
    totalRead += result;
    }
    return result;
    }
    }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    That's a lot of code! I'm too busy now to read that, but I remember file uploading only took a few lines of code when I used Common FileUpload: http://commons.apache.org/fileupload/

    Comment

    Working...