• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

More problems with file uploads

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm haveing trouble uploading files using files with the com.oreilly.servlet package. It fails to recognize any files that I try to upload. The servlet runs without any errors, it just doesn't do anything with the files. The file handling section is identical to the provided example, which does work. My form is also like the example, even using the same field names.
Any ideas? Anybody had the same trouble and fixed it?
Thanks,
Tony.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code for uplodaing and retreving gis and jpgs ..
if u want text files just replce
res.setContentType()/whatever u want either text/plain or text/application..

import java.io.*;
import java.sql.*;
/* If you are compiling and executing file ImageJDBC.java in c:\
* the file rao.gif should exist in c:\ (same directory)...
*/
public class ImageJDBC {
public static void main(String args []) throws SQLException, ClassNotFoundException,Exception {
try {

Class.forName ("oracle.jdbc.driver.OracleDriver");
String url = "jdbc racle:thin:@rao:1521:amica";
Connection conn = DriverManager.getConnection(url,"mail","mail");
// method to insert Image into database
ImageJDBC obj = new ImageJDBC();
obj.insertImage(conn);
// method to retreive Image from database
obj.getImage(conn);
}catch(Exception e){
e.printStackTrace();
}
}
public void insertImage(Connection conn){

try {

Statement stmt = conn.createStatement ();
// execute CREATE for webimages table
boolean flag = stmt.execute("CREATE TABLE webimages (img_ident VARCHAR2 (64), img_fmt CHAR (5), img_data LONG RAW)");
// Insert data into the img_data column
File file = new File ("rao.gif");
InputStream istr = new FileInputStream ("rao.gif");
// prepare the INSERT into webimages using parameters
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO webimages (img_ident, img_fmt, img_data) VALUES (?, ?, ?)");
pstmt.setString (1, "Rao Logo");
pstmt.setString (2, "GIF");
pstmt.setBinaryStream (3, istr, (int)file.length ());
pstmt.execute ();
}catch(Exception e){
e.printStackTrace();
}
}
public void getImage(Connection conn){
try {
Statement stmt = conn.createStatement ();
// retrieve image from row when the ID is 'Rao Logo'
ResultSet rs = stmt.executeQuery ("SELECT img_data FROM webimages WHERE img_ident='Rao Logo'");
// process the ResultSet data
if (rs.next ()) {
// get binary stream data from result set
InputStream web_image = rs.getBinaryStream(1);
// open operating system file for storing the image
FileOutputStream ostr = new FileOutputStream("backlogo.gif");
// fetch-write loop, fetch image data from rs, write to file
int i;
while ((i = web_image.read()) != -1) {
ostr.write (i);
}
ostr.close ();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
 
Tony Kemp
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not the kind of uploading I wasnt to do, I want to upload files across a network using a web browser. I have set everything up, but com.oreilly.servlet.MultiPartParser is not fining the files I try to upload - it just thinks that the form is empty. Here's my processing code:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
out = response.getWriter();
dirName = "/tmp/vlis/";
response.setContentType("text/plain");
System.err.println("Setting up...");
out.println("Files:");
try {
multi = new MultipartRequest(request, dirName); // 1 Mb upload limit
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
System.out.println("Processing file...");
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
FileReader fs = new FileReader(f);
BufferedReader in = new BufferedReader(fs);
String s, filecontent = new String();
while((s = in.readLine())!= null) {
filecontent += s + "\n";
}
//session.setAttribute("fileContent", fileContent);
//out.println(in.readLine());
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("f.toString(): " + f.toString());
out.println("f.getName(): " + f.getName());
out.println("f.exists(): " + f.exists());
out.println("f.length(): " + f.length());
out.println("fileContent: " + filecontent);
}
in.close();
}
} catch (IOException ioe) {
System.out.println("Error processing file: " + ioe.getMessage());
}
}
 
Tony Kemp
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that, let me try again with code tages:
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, multipartRequest is for the Http form posting using the enctype="multipart/form-data". It looks like you try to load and print the local files on the servlet server. If that's the case, regular file IO package can hanlde it. Otherwise, you need to have a form like the following to submit your request:
<form method="post" enctype="multipart/form-data"
action="/servlet/yourservlet">
<input type=file name="">
</form>
Hope this helps

Originally posted by Tony Kemp:
Sorry about that, let me try again with code tages:

 
Tony Kemp
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I'm doing. I have an HTML page with a multipart/formdata form on it, with a file element (called "file1") as the only input, and a submit button, with the action pointing to the servlet containing the code included above, using the POST method. And it just doesn't work
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony- I know what you're going through- but I did get mine working- a question that might I think you need to ask is whether its deployed properly- can you post your deployment descriptor(web.xml entries), as well as the HTMl Post tag where you are posting to? I guess the first questions is- does it even get to the servlet(ie. is it deployed properly). Forgive me if I didn't gleam this from the other posts.
 
Tony Kemp
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it does get to the servlet OK, it prints out "Files:" as per the first print statements, but it doesn't access the file that I upload, it just sails on through as though there were no files specified.
Here is the form:

And the relevant section of web.xml:

Does this give you any ideas?
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
multi.getFileNames() method returns nothing: it can not detect the name of the file as provided by the form:
You forgot to set the NAME of the file parameter in your form, try <input type=FILE NAME=file1> instead of <input type=FILE value=file1>.
HTH
 
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your html needs to look like this:
<FORM ACTION="/servlet/MyServlet"
ENCTYPE="multipart/form-data"
METHOD=POST>
What is your name? <INPUT TYPE=TEXT NAME=submitter><br>
What file are you sending?
<INPUT TYPE=FILE NAME=secretDocument><br>
<input type=hidden name="foo" value="bar">
<input type="submit" value=Submit><br>
</FORM>
 
Tony Kemp
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. Damn, I can't beleive I did that. I hate it when I overlook silly little things like that .
Thank you everyone for your help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic