Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

NEED HELP! I want to post a file thru HTTP to be processed on my Server Side APIs.  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I desperate need help.

I am trying to load a file from my client application to send to my server side API though a URL connection.

The problem is that on the server side i am not able to catch the inputstream in the response. Can anyone please help, am I doing this correctly?

here is my code

CLIENT SIDE=>

url = new URL(apiurl + "?method=transfer.uploadfile&userauth=" + userauth+ "&apikey=" + apikey+ "&transfer=" +transferid+
"&filename=" + file.getName() + "&apisig=" + signature);

urlConn = url.openConnection();
urlConn.setDoOutput(true);

OutputStream out = urlConn.getOutputStream();
FileInputStream in = new FileInputStream(file);

int len;
byte[] buffer = new byte[1024];
while((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}

in.close();
out.flush();
out.close();


Server side=>

ServletInputStream in = req.getInputStream();
//Iterator it = this.upload.parseRequest(req).iterator();
//FileItem item;
String uploadFileName;
String uploadFilePath;
FileOutputStream outputStream;
//ServletOutputStream out = res.getOutputStream();
// initialize a file to hold the uploaded data
uploadFileName = System.currentTimeMillis()+"-"+Math.round(Math.random()*1000000);
uploadFilePath = this.tempDir+ File.separator +"complete" + File.separator +uploadFileName;
File uploadFile = new File(uploadFilePath);
outputStream = new FileOutputStream(uploadFile);

// save to temp file
int len;
byte[] buffer = new byte[1024];
while((len = in.read(buffer)) != -1)
{
outputStream.write(buffer,0,len);
System.out.println("<<<<<<" + len);
}

in.close();
outputStream.flush();
outputStream.close();
 
Bartender
Posts: 9509
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uploading a file via HTTP is a little more involved than the code you've written. Have a look at our FAQ entry FileUpload for some utilities that can help you along.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!