Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 27th, 2005, 8:27 AM   #1
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
Not finished but close enough.......

I was absenmindly at work trying to figure out a problem for uploading files to the webserver via http/web forms so i used the glories of the internet and found a few API's which all were non-comercial licences, so i had to re-invent how to do this which was nice so here it is

my uploader form using Java:

toUpload.html
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="GENERATOR" content="IBM WebSphere Studio" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="theme/Master.css"
	rel="stylesheet" type="text/css" />
<title>toUpload.html</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="fileUpload.jsp">
File: <input type="file" name="file"><br>
<input value="Submit" type="submit">
</form>
</body>
</html>

fileUpload.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ page 
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="AIS.FileUploader" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="GENERATOR" content="IBM WebSphere Studio" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="theme/Master.css"
	rel="stylesheet" type="text/css" />
<title>fileUpload.jsp</title>
</head>
<body>
<%
try{
	BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
	
	FileUploader fUp = new FileUploader();
	fUp.uploadFile(bis); 
		
}
catch(Exception e){
	e.printStackTrace();
}
%>
</body>
</html>

FileUploader.Java

package AIS;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;

import connectInfo.dbConnect;
/**
 * @author alex craddock
 * PaperLess Production System
 * 
 */
public class FileUploader {

	private String fileName = "";
	private String[] list = {"jpg","gif","bmp"};
	private StringBuffer firstLine = new StringBuffer();
	private StringBuffer secondLine = new StringBuffer();
	private StringBuffer thirdLine = new StringBuffer();	
	private String extension = "";
	private String fullName = "";
	
	public FileUploader(){};
	
	public void getHeader(BufferedInputStream fis) throws IOException{//gets the first 3 lines of the header of the request
		
		int disp_counter=0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[8192];
		
		byte[] b = new byte[1];
		StringBuffer sb = new StringBuffer();
		int i = 0;

		try{
			while (i < 3){
				switch (i){
					case 0:
						fis.read(b);
						if (b[0] == 13){
							i++;
						}else{
							firstLine.append(new String(b));
						}
						break;
					case 1:
						fis.read(b);
						if (b[0] == 13){
							i++;
						}else{
							secondLine.append(new String(b));
						}
			
						break;
					case 2:
						fis.read(b);
						if (b[0] == 13){
							i++;
						}else{
							thirdLine.append(new String(b));
						}
			
						break;
				}
			}
			fis.read(b);	
			fis.read(b);
			fis.read(b);
		}catch(IOException e){
			e.printStackTrace();
		}
		
		bos.close();		
		getFname();
		
	}

	public String getFname(){//gets the name of the file you are uploading
			
		int oldPos = 0;
		String getFile = secondLine.toString();
		int pos = getFile.indexOf("filename=");
		String subString = getFile.substring((pos + 10),(getFile.length() - 1));
		while (pos != -1){
			oldPos = pos;
			pos = subString.indexOf("\\");
			if (pos != -1){
				subString = subString.substring((pos+1));
			}
		}

		this.fullName = subString;
		pos = subString.indexOf(".");
			
		this.extension = subString.substring(pos + 1);
		return this.fileName;	
	}
	
	public String getExtension(){//gets the extension eg .exe .jpg .txt etc..
		return this.extension;
	}
	
	public String getFullName(){
		return this.fullName;
	}
	
	public void uploadFile(BufferedInputStream bis) throws Exception{//where the file gets copied
		
		OutputStream bos = null;
		dbConnect dbc = new dbConnect();
		ResultSet rs = null;
		
		try{
			
			FileUploader fUp = new FileUploader();
			getHeader(bis);
			rs = dbc.getBPCS("SELECT * FROM NOTIFIER.DEFINE");//a work thing
			rs.next();
			String sLoc = rs.getString(2);
			String sPicLoc = rs.getString(3);
//System.out.println(sLoc.trim() + sPicLoc.trim());			
			
			bos = new FileOutputStream("C:\\Documents and Settings\\Alex Craddock\\My Documents\\IBM\\wssd\\workspace\\Paperless Production System\\Web Content\\picture\\" 
										+ this.getFullName(),true);//directory where to put the file
			
			byte[] buffer = new byte[8192];
			int bytesRead = 0;
		
		  	while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
		    	bos.write(buffer, 0, bytesRead);
		    }
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			if (bos != null){
				bos.close();
			}
			if (bis != null){
				bis.close();	
			}
			if (dbc != null){
				dbc.closeCon();	
			}
		}
	}	
}

Unfortunatly I cant show you it working as its up on the server at work
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jul 27th, 2005, 8:39 AM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Side story: I remember back in advanced web class we had to do a JSP upload like this, but the form contained text, checkbox, and file inputs. The annoying thing was that when you did multipart/form-data, it would trash all inputs except the file...
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC