![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|