Sending request from Java to Box for uploading files - java

I want to upload file to Box using Java HTTP RESTFUL requests.
My code is,
File file = new File("C:/Users/Developer/Desktop/cloud.ppt");
Base64.Encoder encoder = Base64.getEncoder();
Base64.Decoder decoder = Base64.getDecoder();
String boundary1 = "----------------------------741e90d31eff";
String header1 = "--"+boundary1+"\nContent-Disposition: form-data; name=\"file\"; filename="+file.getName()+";\nContent-Type: application/octet-stream";
System.out.println("=====header1======="+header1);
String footer1 = "--"+boundary1+"--";
String header2= header1;
header1 = header1+"\r\n\r\n";
byte[] byteConvert = header1.getBytes("UTF-8");
String headerEncoded1 = encoder.encodeToString(byteConvert);
HttpResponse response;
System.out.println("******headerEncoded1*****"+headerEncoded1);
byte[] byteConvert2 = header2.getBytes("UTF-8");
while(headerEncoded1.endsWith("="))
{
header2+=' ';
header2 = header2+"\r\n\r\n";
byteConvert2 = header2.getBytes("UTF-8");
headerEncoded1 = encoder.encodeToString(byteConvert2);
}
byte fileContent[] = new byte[(int)file.length()];
System.out.println("fileContent[]: " +fileContent);
FileInputStream fin = new FileInputStream(file);
fin.read(fileContent);
fin.close();
String fileContentString = new String(fileContent);
String fileBody = fileContentString;
byteConvert2 = fileBody.getBytes("UTF-8");
System.out.println("byteConvert2: " +byteConvert2);
String bodyEncoded1 = encoder.encodeToString(byteConvert2);
System.out.println("***bodyEncoded1***"+bodyEncoded1);
String bodyBlob = null;
String last4Bytes = bodyEncoded1.substring(bodyEncoded1.length()-4,bodyEncoded1.length());
System.out.println("****last4Bytes****"+last4Bytes);
if(last4Bytes.endsWith("=="))
{
System.out.println("=== if ===");
last4Bytes = last4Bytes.substring(0,2) + "0K";
bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes;
byteConvert2 = footer1.getBytes("UTF-8");;
String footerEncoded = encoder.encodeToString(byteConvert2);
String Combine = headerEncoded1+bodyEncoded1+footerEncoded;
byteConvert2 = Combine.getBytes("UTF-8");
byte[] byteConvert3 = decoder.decode(byteConvert2);
bodyBlob = byteConvert3.toString();
System.out.println("\n***if bodyBlob***"+bodyBlob);
}
else if(last4Bytes.endsWith("="))
{
System.out.println("===else if bodyBlob===");
last4Bytes = last4Bytes.substring(0,3) + "N";
bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes;
footer1 = "\n" + footer1;
byteConvert2 = footer1.getBytes("UTF-8");
String footerEncoded = encoder.encodeToString(byteConvert2);
String strCombineNew= headerEncoded1+bodyEncoded1+footerEncoded;
byteConvert2 = strCombineNew.getBytes("UTF-8");
byte[] byteConvert3 = decoder.decode(byteConvert2);
bodyBlob = byteConvert3.toString();
System.out.println("\n****else if bodyBlob***"+bodyBlob);
}
else
{
System.out.println("===else===");
footer1 = "\r\n" + footer1;
byteConvert2 = footer1.getBytes("UTF-8");
String footerEncoded = encoder.encodeToString(byteConvert2);
String strCombine = headerEncoded1+bodyEncoded1+footerEncoded;
byteConvert2 = strCombine.getBytes("UTF-8");
byte[] byteConvert3 = decoder.decode(byteConvert2);
bodyBlob = byteConvert3.toString();
System.out.println("\n***else***"+bodyBlob);
}
//System.out.println("****bodyBlob***"+bodyBlob);
//java.sql.Blob blob = org.hibernate.Hibernate.createBlob(bodyBlob.getBytes());
//System.out.println("=====blob======"+blob);
String strFolderId = "XXXXXXX";
HttpClient httpclient = HttpClientBuilder.create().build();
String sUrl = "https://upload.box.com/api/2.0/files/content?parent_id="+strFolderId;
HttpPost request = new HttpPost(sUrl);
request.setEntity(new StringEntity(bodyBlob));
request.setHeader("Content-Type","multipart/form-data; boundary="+boundary1);
request.setHeader("Authorization", "Bearer" +strAccessTokenOnly);
System.out.println("\n=== Content-Length ===" +String.valueOf(fileBody.length()));
request.setHeader("Content-Length",String.valueOf(fileBody.length()));
response = httpclient.execute(request);
System.out.println("\n=== response ==="+response.getStatusLine());
Here, If I set header with Content-Length, the execution will stop in between.
And If I did not add the setHeader I am getting as Bad Request, Status 400.
If anyone have worked on related to this scenario, please help me..
Thanks in advance

Related

HttpGet request to the Binance exchange error with java

I have a trouble with send HTTP Get request via Binance exchange.
(I need to return my wallet status)
the GitHub manual says that (https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md)
Account information (USER_DATA)
GET /api/v3/account (HMAC SHA256)
Get current account information.
Weight: 5
Parameters:
Name Type Mandatory Description
recvWindow LONG NO
timestamp LONG YES
my codes are as shown follows
public static String timestamp = String.valueOf(System.currentTimeMillis());
public static void wallet_status () throws NoSuchAlgorithmException, InvalidKeyException {
String url = "https://api.binance.com/api/v3/account&timestamp=" + timestamp;
//sign url
Mac shaMac = Mac.getInstance("HmacSHA256");
SecretKeySpec keySpec = new SecretKeySpec(BINANCE_SECRET_KEY.getBytes(), "HmacSHA256");
shaMac.init(keySpec);
final byte[] macData = shaMac.doFinal(url.getBytes());
String sign = Hex.encodeHexString(macData);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://api.binance.com/api/v3/account"+"?timestamp="+timestamp+"?signature="+sign);
request.addHeader("X-MBX-APIKEY", BINANCE_API_KEY);
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream stream = entity.getContent()) {
BufferedReader reader =
new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} //end
the server response is like below
{"code":-1100,"msg":"Illegal characters found in parameter 'timestamp'; legal range is '^[0-9]{1,20}$'."}
but my String timestamp is a 13 digit numeric string which should be no problem. please help.
Your url is wrong. Change ?signature= to &signature=.
You have to use & as the delimeter for subsequent variables in a URL. Currently, the ?signature... is seen as the value of the timestamp variable, causing that error message.
Query string delimiter is & not ?
Use: "https://api.binance.com/api/v3/account"+"?timestamp="+timestamp+"&signature="+sign
This Post is really old, but maybe someone will help this solution:
// Binance testnet Data
private String baseUrl = "https://api.binance.com";
private String apiKey = "you API Key";
private String apiSecret = "Your Secret";
private String endpoint = "/api/v3/account";
private String parameters = "recvWindow=20000&timestamp=" + System.currentTimeMillis();
public void getData() throws Exception {
byte[] bytes = hmac("HmacSHA256", apiSecret.getBytes(), parameters.getBytes());
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(baseUrl + endpoint + "?" + parameters + "&signature=" + bytesToHex(bytes)))
.setHeader("X-MBX-APIKEY", apiKey)
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
// print status code
System.out.println(response.statusCode());
// print response body
System.out.println(response.body());
}
public static byte[] hmac(String algorithm, byte[] key, byte[] message) throws Exception {
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(message);
}
public static String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0, v; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}

image compression using s3 and lambda in java

I am trying to fetch image from s3 bucket of aws in lambda fuction and i want to compress this image and upload back to destination bucket of aws s3. I tried to fetch file from s3 and compress it.but facing problem in uploading the compressed file
Finally this worked
public class TempComp implements RequestHandler<S3Event, String> {
private static final float MAX_WIDTH = 100;
private static final float MAX_HEIGHT = 100;
private final String JPG_TYPE = (String) "jpg";
private final String JPG_MIME = (String) "image/jpeg";
private final String PNG_TYPE = (String) "png";
private final String PNG_MIME = (String) "image/png";
public String handleRequest(S3Event s3event, Context context) {
try {
S3EventNotificationRecord record = s3event.getRecords().get(0);
String srcBucket = record.getS3().getBucket().getName();
// Object key may have spaces or unicode non-ASCII characters.
String srcKey = record.getS3().getObject().getKey()
.replace('+', ' ');
srcKey = URLDecoder.decode(srcKey, "UTF-8");
String dstBucket = srcBucket + "resized";
String dstKey = "resized-" + srcKey;
// Sanity check: validate that source and destination are different
// buckets.
if (srcBucket.equals(dstBucket)) {
System.out
.println("Destination bucket must not match source bucket.");
return "";
}
// Infer the image type.
Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
if (!matcher.matches()) {
System.out.println("Unable to infer image type for key "
+ srcKey);
return "";
}
String imageType = matcher.group(1);
if (!(JPG_TYPE.equals(imageType)) && !(PNG_TYPE.equals(imageType))) {
System.out.println("Skipping non-image " + srcKey);
return "";
}
// Download the image from S3 into a stream
AmazonS3 s3Client = new AmazonS3Client();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(
srcBucket, srcKey));
InputStream objectData = s3Object.getObjectContent();
// int b=objectData.read();
// Read the source image
BufferedImage srcImage = ImageIO.read(objectData);
// BufferedImage destImage = srcImage;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
ByteArrayOutputStream os=new ByteArrayOutputStream() ;
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
// compress to a given quality
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.5f);
// appends a complete image stream containing a single image and
//associated stream and image metadata and thumbnails to the output
writer.write(null, new IIOImage(srcImage, null, null), param);
System.out.print("file is compressed");
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
//InputStream is = new ByteArrayInputStream(retValue);
BufferedImage destImage = ImageIO.read(is);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
ImageIO.write(destImage, imageType, os1);
InputStream is1 = new ByteArrayInputStream(os1.toByteArray());
// Set Content-Length and Content-Type
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(os1.size());
System.out.println(os1.size());
if (JPG_TYPE.equals(imageType)) {
meta.setContentType(JPG_MIME);
}
if (PNG_TYPE.equals(imageType)) {
meta.setContentType(PNG_MIME);
}
// Uploading to S3 destination bucket
System.out.println("Writing to: " + dstBucket + "/" + dstKey);
s3Client.putObject(dstBucket, dstKey, is1, meta);
System.out.println("Successfully resized " + srcBucket + "/"
+ srcKey + " and uploaded to " + dstBucket + "/" + dstKey);
return "Ok";
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Send HTTP Response from we server to client

I have a problem in sending the body of http response,and I think the problem in this line out.write(buffer, 0, bytes); please help me .
DataInputStream din = new DataInputStream(ClientConn.getInputStream());
OutputStream ot = ClientConn.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(ot);
String request = din.readLine().trim();
System.out.println(request);
StringTokenizer st = new StringTokenizer(request);
String header = st.nextToken();
System.out.println(header);
if (header.equals("GET")) {
String fileName = st.nextToken();
String file = fileName.substring(1, fileName.length());
System.out.println(file);
FileInputStream fin = null;
boolean fileExist = true;
try {
fin = new FileInputStream(file);
}
catch (Exception ex) {
fileExist = false;
}
String ServerLine = "Simple HTTP Server";
String StatusLine = null;
String ContentTypeLine = null;
String ContentLengthLine = null;
String ContentBody = null;
if (fileExist) {
StatusLine = "HTTP/1.0 200 OK";
ContentTypeLine = "Content-type: text/html";
ContentLengthLine = "Content-Length: " + (new Integer(fin.available()).toString());
} else {
StatusLine = "HTTP/1.0 200 OK";
ContentTypeLine = "Content-type: text/html";
ContentBody = "<HTML>" +
"<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
"<BODY>404 Not Found" +
"</BODY></HTML>";
ContentLengthLine = (new Integer(ContentBody.length()).toString());
}
out.write(StatusLine.getBytes());
out.write(ServerLine.getBytes());
out.write(ContentTypeLine.getBytes());
out.write(ContentLengthLine.getBytes());
// output.writeUTF(file);
if (fileExist) {
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
for (int iCount = 0; iCount < bytes; iCount++) {
int temp = buffer[iCount];
System.out.print((char) temp);
}
}
}
out.flush();
fin.close();
} else {
//out.write(ContentBody.getBytes());
}
out.close();
ClientConn.close();
Your header is not properly written into the OutputStream, you forget to write the EOL characters which are "\r\n" at the end of each line. And before starting to write the content of your body you need to write the EOL characters too.
In other words, you need to do something like this:
String eol = "\r\n";
Charset charset = Charset.forName("ASCII");
byte[] eolBytes = eol.getBytes(charset);
out.write(StatusLine.getBytes(charset));
out.write(eolBytes);
out.write( ServerLine.getBytes(charset));
out.write(eolBytes);
out.write(ContentTypeLine.getBytes(charset));
out.write(eolBytes);
out.write( ContentLengthLine.getBytes(charset));
out.write(eolBytes);
// End of the header
out.write(eolBytes);
// Here the body begin
Indeed your header must be encoded into ASCII.
Response update:
Other remarks regarding your code:
Use the method File#exists() to check if a file exist.
Use Files.getAttribute(Paths.get("/path/to/my/file"), "size") to get the size of your file
In case the file doesn't exist you just don't write the body in your current code.

Download a file from an s3 bucket with REST

I am attempting to download from an s3 bucket with REST calls. I am able to create buckets, delete buckets and list buckets. Downloading always ends with either an object not found or HTTP response code: 403. The code is below. Also does anyone know if amazon was sdk can be used with non amazon sites?
public void getObject() throws Exception
{
String fmt = "EEE, dd MMM yyyy HH:mm:ss ";
SimpleDateFormat df = new SimpleDateFormat(fmt, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String ob2 = "/bucket/test.txt";
String bucket = "/bucket";
String method = "GET";
String contentMD5 = "";
String contentType = "";
String date = df.format(new Date()) + "GMT";
// Generate signature
StringBuffer buf = new StringBuffer();
buf.append(method).append("\n");
buf.append(contentMD5).append("\n");
buf.append(contentType).append("\n");
buf.append(date).append("\n");
buf.append(ob2);
String signature = sign(buf.toString());
HttpURLConnection httpConn = null;
URL url = new URL("https”,”s3demo.s3demosite.com",443,bucket);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);
httpConn.setDefaultUseCaches(false);
httpConn.setAllowUserInteraction(true);
httpConn.setRequestMethod(method);
httpConn.setRequestProperty("Date", date);
httpConn.setRequestProperty("Content-Length", "0");
String AWSAuth = "AWS " + keyId + ":" + signature;
httpConn.setRequestProperty("Authorization", AWSAuth);
// Send the HTTP PUT request.
int statusCode = httpConn.getResponseCode();
InputStream err = httpConn.getErrorStream();
InputStream is = null;
is = httpConn.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = err.read()) != -1) {
sb.append((char) ch);
}
if ((statusCode/100) != 2)
{
// Deal with S3 error stream.
InputStream in = httpConn.getErrorStream();
System.out.println("Error: "+errorStr);
}
else
{
System.out.println("download worked”);
}
}

Response :java.net.SocketException: Unexpected end of file from server Java

I got this error from my program when i am call a URL from URLConnector.. the URL is
http://192.168.2.107/cgi-bin/mediaFileFind.cgi?action=findFile&object=27544704&condition.Channel=0&conditon.Dir[0]="/mnt/sd"&condition.StartTime=2014-8-1 00:00:00&condition.EndTime=2014-8-31 23:59:59
but when i capture HTTP using wire-shark then wire-shark the URl is loss
wire-shark capture only
http://192.168.2.107/cgi-bin/mediaFileFind.cgi?action=findFile&object=27544704&condition.Channel=0&conditon.Dir[0]="/mnt/sd"&condition.StartTime=2014-8-1 00:00:00
only this URL
my Java program is
public String intilizeObject(String IP, String user, String pass, String objectID, String dir, String startTime, String endTime) {
String result = "";
try {
String URL = "http://" + IP + "/cgi-bin/mediaFileFind.cgi?action=findFile&object=" + objectID + "&condition.Channel=0&conditon.Dir[0]=\"" + dir + "\"&condition.StartTime=" + startTime + "&condition.EndTime=" + endTime;
String authString = user + ":" + pass;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL url = new URL(URL);
System.out.println(url);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
result = sb.toString();
} catch (Exception e) {
result = e.toString();
}
return result;
}

Categories