• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

doPut

 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I use doPut- I want to upload a file? I can set the method to PUT and override the doPut?
What do I do beyond it? What params I send and how do I actually upload a file.
Thanks
Sanjay
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay,

You do not have to use PUT method. on your front end (Html form), you should set thses attributes,
enctype="multipart/form-data" method=POST
in your server site, either servlet or JSP (I guess) you parse request to get your file content.
Here is what I did.
/*
* HttpUploader.java
*
* Created on October 27, 2001, 8:42 AM
*/
package gn.utils;
/**
*Java Base implementation of RFC 1867 Form-based File Upload in HTML
*Server side processing java bean
* @author Sunny Liu
* @version 2.1
*/
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.http.*;

public class HttpUploader implements Serializable
{
HttpServletRequest request;
HttpServletResponse response;

public HttpUploader (HttpServletRequest request, HttpServletResponse response)
{
init(request, response);
}

public void init(HttpServletRequest request, HttpServletResponse response)
{
this.request = request;
this.response = response;
try{
parserRequest();
}catch(Exception ex){
ex.printStackTrace();
}
}

public String getParameter(String key)
{
String retVal = null;
String[] rets = getParameterValues(key);
if ( rets != null && rets.length >0 ) retVal = rets[0];
return retVal;
}

public String[] getParameterValues(String key)
{
String[] retVal = null;
Object obj = parameters.get(key);
if ( obj != null ) retVal = (String[])obj;
return retVal;
}

public Enumeration getParameterNames()
{
return parameters.keys();
}

public byte[] getFileContent(String key)
{
byte[] retVal = null;
String filename = getParameter(key);
if ( filename != null && files.containsKey(filename)){
retVal = (byte[])files.get(filename);
}
return retVal;
}

public String getFileContentType(String key)
{
String retVal = "application/octet-stream";
String filename = getParameter(key);
if ( filename != null && contentTypes.containsKey(filename)){
retVal = (String)contentTypes.get(filename);
}
return retVal;
}


//maximium allowed upload file size in total 100k == 1MB
private final static long MAX_UPLOAD_SIZE = 1000*1024;
private Hashtable parameters = new Hashtable();
private Hashtable files = new Hashtable();
private Hashtable contentTypes = new Hashtable();

private void parserRequest() throws java.lang.Exception
{

int contentSize = request.getContentLength();
//make sure uploaded size not over limited size;
if ( contentSize <= MAX_UPLOAD_SIZE ){
doParse();
}else{
throw new Exception("File size over limited exception at HttpUploader.");
}
}

private String currFieldName = null;
private String currFileName = null;
private void doParse() throws java.lang.Exception
{
ServletInputStream in = request.getInputStream();
int length = request.getContentLength();
String boundary = null;
String EOR = null;


boolean isEOR = false;
String CRLF = "\r\n";
int readLineLength = 0;
int dataReaded = 0;
byte[] buffer = new byte[length];

//Read boundary line
readLineLength = in.readLine(buffer, 0, length);
if (readLineLength > 0 ){
boundary = new String(buffer, 0, readLineLength);
boundary = boundary.trim();
EOR = boundary + "--";
dataReaded += readLineLength;
}else{
//Should never happen;
throw new java.io.IOException("No data uploaded exception.");
}
//read Each field data;
buffer = new byte[length-dataReaded];

boolean fieldStarted = false;
boolean foundFieldHead = false;
boolean foundCRLF = false;
boolean fieldEnd = false;
boolean isFile = false;

ByteArrayOutputStream temp = new ByteArrayOutputStream();

while ( (readLineLength = in.readLine(buffer, 0, buffer.length)) > 0 )
{

String data = new String(buffer, 0, readLineLength);
dataReaded += readLineLength;
if ( data.trim().startsWith("Content-Disposition:") && !fieldStarted ){
fieldStarted = true;
fieldEnd = false;
parserFieldHeader(data);
}else if (data.trim().startsWith("Content-Type:") && !foundCRLF ){
isFile = true;
parserContentType(data);
}else if (data.equals(CRLF) && !foundCRLF && fieldStarted && !fieldEnd ){
temp = new ByteArrayOutputStream();
foundCRLF = true;
}else if (data.trim().equals(boundary) || data.trim().equals(EOR) ){
if ( isFile ){
if ( currFileName != null ) files.put(currFileName, temp.toByteArray());
}else{
String[] values = null; //values.add(temp.toString());
if( currFieldName != null && parameters.containsKey(currFieldName) ){
String[] oldValues = (String[])parameters.get(currFieldName);
values = new String[oldValues.length + 1];
for (int i = 0; i < oldValues.length; i++) {
values[i] = oldValues[i];
}
values[oldValues.length] = temp.toString();
}else if (currFieldName != null && !parameters.containsKey(currFieldName)){
values = new String[1];
values[0] = temp.toString();
}
if ( currFieldName != null && values != null ) parameters.put(currFieldName, values);
}
currFieldName = null;
currFileName = null;
isFile = false;
foundCRLF = false;
fieldStarted = false;
fieldEnd = true;
}else{
temp.write(buffer, 0, readLineLength);
}

buffer = new byte[length-dataReaded];

}


}

private void parserFieldHeader(String data) throws java.lang.Exception
{

String fieldName = null;
String fileName = null;

int nameIndex = data.indexOf("name=\"");
int nameEnd = data.indexOf("\"", nameIndex+6);
int filenameIndex = data.indexOf("filename=\"");
int fendIndex = data.indexOf("\"", filenameIndex + 10);
if ( nameIndex != -1 && nameEnd > nameIndex ){
fieldName = data.substring(nameIndex+6, nameEnd);
currFieldName = fieldName;
}
if ( filenameIndex != -1 && fendIndex > filenameIndex ){
fileName = data.substring(filenameIndex + 10, fendIndex);
int dos = fileName.lastIndexOf("\\");
int unix = fileName.lastIndexOf("/");
if ( dos != -1 ){
fileName = fileName.substring(dos+1);
}else if (unix != -1 ){
fileName = fileName.substring(unix+1);
}
currFileName = fileName;
}
String[] values = null;
if (parameters.containsKey(fieldName) && fileName != null )
{
String[] oldValues = (String[])parameters.get(fieldName);
values = new String[oldValues.length + 1];
for (int i = 0; i < oldValues.length; i++) {
values[i] = oldValues[i];
}
values[oldValues.length] = fileName;
}else if( !parameters.containsKey(fieldName) && fileName != null){
values = new String[1];
values[0] = fileName;
}
if ( fieldName != null && values != null ) parameters.put(fieldName, values);

}

private void parserContentType(String data)
{
int index = data.indexOf(":");
String type = "application/octet-stream";
if ( index != -1 ){
type = data.substring(index + 1 );
type = type.trim();
}
if ( currFileName != null ) contentTypes.put(currFileName, type);
}
}
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic