0

Need to make a website for a school project.
Decided to make a website like photobucket.

I got every page read, but the only thing missing is the uploading part.
Does anyone know where I can find some examples of uploading blobs?
Or mayb someone can share one.
The thing is that the pictures will need to be stored in the database, and that's causing most of the problems.
The storing and retrieving part.

I pref. not to use servlets, since I will need to explain the code afterwards, and normally a servlet contains a lot more code.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by mindbend
0
<%                        
        String pathoffile="";
        String savepath =application.getRealPath("\\")+"\\Userimage\\";
        String filename = "";
        ServletInputStream in = request.getInputStream();
        byte line[] = new byte[128];
        int i = in.readLine(line, 0, 128);
        int boundarylength = i - 2;
        String boundary = new String(line, 0, boundarylength);
        while (i != -1) 
        {
            String newline = new String(line, 0, i);
            if (newline.startsWith("Content-Disposition: form-data; name=\"")) 
            {
                String s = new String(line, 0, i - 2);
                int pos = s.indexOf("filename=\"");             
                if (pos != -1) 
                {
                    String filepath = s.substring(pos + 10,s.length()-1);
                  pathoffile=filepath;
                    pos=filepath.lastIndexOf("\\");
                    if(pos!=-1)
                    {                                               
                        filename=filepath.substring(pos+1);                         
         filename=filename.replaceAll(" ","_");               
                    } 
                    else 
                    {                         
                     filename = filepath;
                    }
                } 
                else 
                {
                   break;
                }
            //this is the file content
            i = in.readLine(line, 0, 128);
          //System.out.println("1 READING OF I="+i);
            i = in.readLine(line, 0, 128);
            //blank line
            i = in.readLine(line, 0, 128);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            newline = new String(line, 0, i);
            while (i != -1 && !newline.startsWith(boundary)) 
            {
                 //System.out.println("I!=-1 AND NEWLINE NOT STARTS WITH BOUNDARY I="+i);
                //the problem is the last line of the file content
                //contains the new line character
                //so,we need to check if the current line is
                //the last line.
                buffer.write(line, 0, i);
                i = in.readLine(line, 0, 128);
                newline = new String(line, 0, i);
            }
            try 
            {
                //save THE UPLOADED FILE
                String temp=filename;
                filename=id+temp;
                RandomAccessFile f = new RandomAccessFile(savepath + filename,"rw");
                byte bytes[] = buffer.toByteArray();
                f.write(bytes, 0, bytes.length - 2);
                f.close();
            } 
            catch (Exception e) 
            {
               System.out.println(e);  
            }
        }
        i = in.readLine(line, 0, 128);       
    }//end while                      
   %>

Edited by mike_2000_17: Fixed formatting

0

Thanks a lot, but could you also say what lib's I need to import?
It gives me:
Cannot find symbol
location: class SimplifiedJSPServlet

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.