• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Travis Hoang
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();
 
Joe Ess
Bartender
Posts: 9436
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!