473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1487
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

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

Similar topics

4
by: dickiedyce | last post by:
Hi there. I've spent the weekend getting ever more frustrated, trying to get an upload file function working on a website. The site is hosted by a company called oneandone. They're using PHP...
9
by: R. Rajesh Jeba Anbiah | last post by:
Q: How should I handle file upload? A: File uploading requires HTML form of content type "multipart/form-data". The file content has to be POSTed/submitted via the form and once the file is...
5
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
1
by: Jonathan | last post by:
Hi everyone, I have a problem with the file uploading in Asp.Net and I have read a lot on forums on this but never found an answer. Here is the problem: I know Asp.Net maximum Length for...
5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
3
by: Jason Chu | last post by:
I've written a file uploading part of my application using the IHttpModule. So now, I don't have the memory problem of uploading something big. Problem: I can't find which function I have to...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
1
by: wenqiang7 | last post by:
I am encountering a very strang problem with file uploading in my ASP.Net page. When we try to upload certain file, we'll get an error msg of "Cannot find server or DNS Error". We are running...
3
ganesanji
by: ganesanji | last post by:
hi all, I have written a php coding for uploading a file to a specific folder or location in server which is a Linux server. I think the coding for file uploaing is correct. But it does not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.