Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 26th, 2003, 05:57 AM
bmcnicoll bmcnicoll is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 12 bmcnicoll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
file upload in jsp

I am trying to upload a file to a mysql database (using a jsp tomcat 4.1 container) for each new member of my website (typically a cv which is a .rtf word file). I have used the example on the oreilly page and have managed to get the image of the file I have uploaded. Now I am trying to save this into my database for each member so that they can enter all of their details on the one page i.e. name age etc and also a browse for file button which will store the path of their file on their computer. When they click the submit button, all of their details are then stored into the database by the resultant page which outputs wehter they have been successful or not.

The upload bean code (from the oreilly site):

package com.idhcitip;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileUploadBean {

private String savePath, filepath, filename, contentType;
private Dictionary fields;

public String getFilename() {
return filename;
}

public String getFilepath() {
return filepath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String getContentType() {
return contentType;
}

public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null)
return null;
return (String) fields.get(fieldName);
}

private void setFilename(String s) {
if (s==null)
return;

int pos = s.indexOf("filename=\"");
if (pos != -1) {
filepath = s.substring(pos+10, s.length()-1);
// Windows browsers include the full path on the client
// But Linux/Unix and Mac browsers only send the filename
// test if this is from a Windows browser
pos = filepath.lastIndexOf("\\");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
}
private void setContentType(String s) {
if (s==null)
return;

int pos = s.indexOf(": ");
if (pos != -1)
contentType = s.substring(pos+2, s.length());
}

public void doUpload(HttpServletRequest request) throws IOException {
ServletInputStream in = request.getInputStream();

byte[] line = new byte[128];
int i = in.readLine(line, 0, 128);
if (i < 3)
return;
int boundaryLength = i - 2;

String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
fields = new Hashtable();

while (i != -1) {
String newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
if (newLine.indexOf("filename=\"") != -1) {
setFilename(new String(line, 0, i-2));
if (filename==null)
return;
//this is the file content
i = in.readLine(line, 0, 128);
setContentType(new String(line, 0, i-2));
i = in.readLine(line, 0, 128);
// blank line
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter((savePath==null? "" : savePath) + filename)));
while (i != -1 && !newLine.startsWith(boundary)) {
// 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.
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary)))
pw.print(newLine.substring(0, newLine.length()-2));
else
pw.print(newLine);
newLine = new String(line, 0, i);

}
pw.close();

}
else {
//this is a field
// get the field name
int pos = newLine.indexOf("name=\"");
String fieldName = newLine.substring(pos+6, newLine.length()-3);
//System.out.println("fieldName:" + fieldName);
// blank line
i = in.readLine(line, 0, 128);
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
StringBuffer fieldValue = new StringBuffer(128);
while (i != -1 && !newLine.startsWith(boundary)) {
// The last line of the field
// contains the new line character.
// So, we need to check if the current line is
// the last line.
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary)))
fieldValue.append(newLine.substring(0, newLine.length()-2));
else
fieldValue.append(newLine);
newLine = new String(line, 0, i);
}
//System.out.println("fieldValue:" + fieldValue.toString());
fields.put(fieldName, fieldValue.toString());
}
}
i = in.readLine(line, 0, 128);

} // end while
}



This works fine and I can access the image name of the file by using the commands in my jsp code(on the resultant page of the new member form):

<%@ page import="java.sql.*, com.idhcitip.*"%>

<jsp:useBean id="TheBean" scope="page" class="com.idhcitip.FileUploadBean" />
<%
TheBean.doUpload(request);
out.println("Filename:" + TheBean.getFilename());

So I am wondering how can I then store this image into a text blob in the database?

I found some code on the java.sun forum, but am not entirely sure how I am meant to use it?

package com.idhcitip;
import java.sql.*;
import java.io.*;
class BlobTest {
public static void main(String args[]) {
try {

//File to be created. Original file is duke.gif.
//DriverManager.registerDriver( new org.gjt.mm.mysql.Driver());
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String host="localhost";
String user="root";
String pass="";
String db="idhcitip";
String conn;


// create connection string
conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +
pass;

// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);

// query statement
Statement SQLStatement = Conn.createStatement();
/*PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobTest VALUES( ?, ? )" );
pstmt.setString( 1, "photo1");
File imageFile = new File("duke.gif");
InputStream is = new FileInputStream(imageFile);
pstmt.setBinaryStream( 2, is, (int)(imageFile.length()));
pstmt.executeUpdate();
*/
PreparedStatement pstmt = Conn.prepareStatement("SELECT image FROM testblob WHERE Name = ?");
pstmt.setString(1, args[0]);
RandomAccessFile raf = new RandomAccessFile(args[0],"rw");
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
Blob blob = rs.getBlob(1);
int length = (int)blob.length();
byte [] _blob = blob.getBytes(1, length);
raf.write(_blob);
}
System.out.println("Completed...");
} catch(Exception e) {
System.out.println(e);
}
}
}

Once I have managed to store the file, I would just like people to be able to view each members profile and then to click on a link that will have their stored c.v.

Reply With Quote
  #2  
Old December 2nd, 2003, 06:22 AM
sureshmulagala sureshmulagala is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 3 sureshmulagala User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy

Hi,

I am trying to code a jsp page similar to the previous one...but upload gifs or jpegs....can anyone please send me the code for it?

Help will be highly appreciated.....I had tried something similar to the previous one posted...but it would corrupt the image if i upload jpeg or gifs.....

Thanks
Suresh

Reply With Quote
  #3  
Old December 3rd, 2003, 09:01 AM
e4c5 e4c5 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Posts: 778 e4c5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 23 m 20 sec
Reputation Power: 6
Hi,
There are several components for file upload with j2ee including at least two by the jakarta project. JSPUpload (at sourceforge) is my favourite.

--
http://www.radinks.com/upload/
Drag and Drop file upload applet.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > file upload in jsp


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT