uploading file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArwaAbood
    New Member
    • Jul 2007
    • 4

    #1

    uploading file

    I want to upload file using the following code to make the form:

    [code=html]
    <html>
    <head>
    <script type="text/javascript">
    function redirect()
    {
    alert(msg+" want to add new file");
    location.href=" ProcessFile.jsp ?p=1"
    }
    </script>
    <title>Adding page</title>
    </head>
    <body>
    <form name="uploadFor m" enctype="multip art/form-data" method="post" action="Process File.jsp?p=0">
    <input type="file" name="file">
    <input TYPE=Button name='Upload' value="Add New File" Click="redirect ()">
    <input TYPE="submit" value="Finish" name="submit" >
    <input type="reset" value="Reset" name="reset">
    </form>
    </body>
    </html>[/code]

    when I press "Finish" the next page is executing correct.when I press "Add New File" the file must be uploaded and return to the Previous to re_enter another file,but this not Happened and this exception is occur(Ilegal ENCTYPE : must be multipart/form-data ENCTYPE set = null )

    The (ProcessFile.js p) code is;
    [code=java]
    <%
    response.setCon tentType("text/html");
    response.setHea der("Cache-control","no-cache");

    String lastFileName = "";

    String contentType = request.getCont entType();
    String boundary = "";
    final int BOUNDARY_WORD_S IZE = "boundary=".len gth();
    if(contentType == null || !contentType.st artsWith("multi part/form-data")) {
    err = "Ilegal ENCTYPE : must be multipart/form-data\n";
    err += "ENCTYPE set = " + contentType;
    }else{
    boundary = contentType.sub string(contentT ype.indexOf("bo undary=") + BOUNDARY_WORD_S IZE);
    boundary = "--" + boundary;
    try {
    javax.servlet.S ervletInputStre am sis = request.getInpu tStream();
    byte[] b = new byte[1024];
    int x=0;
    int state=0;
    String name=null,fileN ame=null,conten tType2=null;
    java.io.FileOut putStream buffer = null;
    while((x=sis.re adLine(b,0,1024 ))>-1) {
    String s = new String(b,0,x);
    if(s.startsWith (boundary)) {
    state = 0;
    name = null;
    contentType2 = null;
    fileName = null;
    }else if(s.startsWith ("Content-Disposition") && state==0) {
    state = 1;
    if(s.indexOf("f ilename=") == -1)
    name=s.substrin g(s.indexOf("na me=")+"name=".l ength(),s.lengt h()-2);
    else {
    name=s.substrin g(s.indexOf("na me=")+"name=".l ength(),s.lastI ndexOf(";"));
    fileName=s.subs tring(s.indexOf ("filename=" )+ "filename=".len gth(),s.length( )-2);
    if(fileName.equ als("\"\"")) {
    fileName = null;
    }else {
    String userAgent = request.getHead er("User-Agent");
    String userSeparator="/"; // default
    if (userAgent.inde xOf("Windows")! =-1)
    userSeparator=" \\";
    if (userAgent.inde xOf("Linux")!=-1)
    userSeparator="/";
    fileName = fileName.substr ing(fileName.la stIndexOf(userS eparator)+1,fil eName.length()-1);
    if(fileName.sta rtsWith( "\""))
    fileName = fileName.substr ing( 1);
    }
    }
    name = name.substring( 1,name.length()-1);
    if (name.equals("f ile"))
    {
    if (buffer!=null)
    buffer.close();
    lastFileName = fileName;
    buffer = new java.io.FileOut putStream("c:\\ "+fileName) ;
    }
    }else if(s.startsWith ("Content-Type") && state==1) {
    state = 2;
    contentType2 = s.substring(s.i ndexOf(":")+2,s .length()-2);
    }else if(s.equals("\r \n") && state != 3) {
    state = 3;
    }else {
    if (name.equals("f ile"))
    buffer.write(b, 0,x);
    }

    }
    sis.close();
    buffer.close();
    boolean ok = err.equals("");
    if(!ok) {
    out.println(err );
    }%>[/code]

    please help me
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, ArwaAbood. Welcome to TSDN!

    I'm going to go ahead and move this thread to the Java forum, where one of our resident Experts will be more likely to see it.

    Comment

    • ArwaAbood
      New Member
      • Jul 2007
      • 4

      #3
      Thanks alot pbmods for helping me

      Comment

      Working...