469,890 Members | 2,321 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

uploading file

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <script type="text/javascript">
  4.     function redirect()
  5.     {
  6.          alert(msg+" want to add new file");
  7.              location.href="ProcessFile.jsp?p=1"
  8.     }
  9.     </script>
  10.     <title>Adding page</title>
  11. </head>
  12. <body>
  13. <form name="uploadForm" enctype="multipart/form-data" method="post" action="ProcessFile.jsp?p=0">
  14.      <input type="file" name="file">
  15.      <input TYPE=Button name='Upload' value="Add New File" Click="redirect()">
  16.      <input TYPE="submit" value="Finish" name="submit" >
  17.      <input type="reset" value="Reset" name="reset">
  18. </form>
  19. </body>
  20. </html>
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.jsp) code is;
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     response.setContentType("text/html");
  3.     response.setHeader("Cache-control","no-cache");
  4.  
  5.     String lastFileName = "";
  6.  
  7.       String contentType = request.getContentType();
  8.     String boundary = "";
  9.     final int BOUNDARY_WORD_SIZE = "boundary=".length();
  10.     if(contentType == null || !contentType.startsWith("multipart/form-data")) {
  11.       err = "Ilegal ENCTYPE : must be multipart/form-data\n";
  12.       err += "ENCTYPE set = " + contentType;
  13.     }else{
  14.       boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
  15.       boundary = "--" + boundary;
  16.       try {
  17.         javax.servlet.ServletInputStream sis = request.getInputStream();
  18.         byte[] b = new byte[1024];
  19.         int x=0;
  20.         int state=0;
  21.         String name=null,fileName=null,contentType2=null;
  22.         java.io.FileOutputStream buffer = null;
  23.         while((x=sis.readLine(b,0,1024))>-1) {
  24.           String s = new String(b,0,x);
  25.           if(s.startsWith(boundary)) {
  26.             state = 0;
  27.             name = null;
  28.             contentType2 = null;
  29.             fileName = null;
  30.           }else if(s.startsWith("Content-Disposition") && state==0) {
  31.             state = 1;
  32.             if(s.indexOf("filename=") == -1)
  33.               name=s.substring(s.indexOf("name=")+"name=".length(),s.length()-2);
  34.             else {
  35.       name=s.substring(s.indexOf("name=")+"name=".length(),s.lastIndexOf(";"));
  36.      fileName=s.substring(s.indexOf("filename=")+   "filename=".length(),s.length()-2);
  37.               if(fileName.equals("\"\"")) {
  38.                 fileName = null;
  39.               }else {
  40.                 String userAgent = request.getHeader("User-Agent");
  41.                 String userSeparator="/";  // default
  42.                 if (userAgent.indexOf("Windows")!=-1)
  43.                   userSeparator="\\";
  44.                 if (userAgent.indexOf("Linux")!=-1)
  45.                   userSeparator="/";
  46.                 fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
  47.                 if(fileName.startsWith( "\""))
  48.                   fileName = fileName.substring( 1);
  49.               }
  50.             }
  51.             name = name.substring(1,name.length()-1);
  52.             if (name.equals("file"))
  53.            {
  54.               if (buffer!=null)
  55.                 buffer.close();
  56.               lastFileName = fileName;
  57.               buffer = new java.io.FileOutputStream("c:\\"+fileName);
  58.             }
  59.           }else if(s.startsWith("Content-Type") && state==1) {
  60.             state = 2;
  61.             contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
  62.           }else if(s.equals("\r\n") && state != 3) {
  63.             state = 3;
  64.           }else {
  65.             if (name.equals("file"))
  66.               buffer.write(b,0,x);
  67.           }
  68.  
  69.         }
  70.         sis.close();
  71.         buffer.close();
  72.       boolean ok = err.equals("");
  73.       if(!ok) {
  74.         out.println(err);
  75.       }%>
please help me
Jul 12 '07 #1
2 1254
pbmods
5,821 Expert 4TB
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.
Jul 12 '07 #2
Thanks alot pbmods for helping me
Jul 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by dickiedyce | last post: by
9 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
5 posts views Thread by Kevin Ollivier | last post: by
1 post views Thread by Jonathan | last post: by
3 posts views Thread by Jason Chu | last post: by
5 posts views Thread by Chris | last post: by
1 post views Thread by wenqiang7 | last post: by
ganesanji
3 posts views Thread by ganesanji | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.