Sample C# code:
static void UploadFile(string sasUrl, string filepath)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("x-ms-version", Version);
client.DefaultRequestHeaders.Add("x-ms-client-request-id", SessionGuid);
StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?><BlockList>");
foreach (byte[] chunk in GetFileChunks(filepath))
{
var blockid = GetHash(chunk);
HttpRequestMessage chunkMessage = new HttpRequestMessage()
{
Method = HttpMethod.Put,
RequestUri = new Uri(sasUrl + "&timeout=90&comp=block&blockid=" + WebUtility.UrlEncode(blockid)),
Content = new ByteArrayContent(chunk)
};
chunkMessage.Headers.Add("x-ms-blob-type", "BlockBlob");
chunkMessage.Content.Headers.Add("MD5-Content", blockid);
TimeAction("Uploading chunk " + blockid + " took {0} ms", () =>
{
var response = client.SendAsync(chunkMessage).Result;
});
sb.Append("<Latest>");
sb.Append(blockid);
sb.Append("</Latest>");
}
sb.Append("</BlockList>");
Trace.WriteLine(sb.ToString());
HttpRequestMessage commitMessage = new HttpRequestMessage()
{
Method = HttpMethod.Put,
RequestUri = new Uri(sasUrl + "&timeout=90&comp=blocklist"),
Content = new StringContent(sb.ToString())
};
TimeAction("Commiting the blocks took {0} ms", () =>
{
var commit = client.SendAsync(commitMessage).Result;
});
}
}
I am stuck at the point where I've to upload a file. Also want to know what the reason is to commit in given code?
my progress so far is :
public static void uploadFile(String sasUrl , String filepath , String sessionGuid)
{
File file = new File(filepath);
FileInputStream fileInputStream=null;
Response reply = new Response();
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(sasUrl);
request.setHeader("x-ms-version", "2013-08-15");
request.setHeader("x-ms-client-request-id", sessionGuid);
StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?><BlockList>");
}
}
Note: I cannot run the code multiple times as I cannot spam the server. Any suggestions will be appreciated
Referring to : https://msdn.microsoft.com/en-us/library/windows/hardware/dn800660(v=vs.85).aspx
According to the reference code in C#, it seems to be using the REST API Put Block List to upload a file as a block blob.
So you can refer to the REST API reference without refering to the C# sample to use httpclient to construct the request for uploading.
However, the simple way is using Azure Storage SDK for Java. To upload a file, you just need to use the class CloudBlockBlob to upload a file with the function upload(InputStream sourceStream, long length), please refer to the tutorial https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-blob-storage/#upload-a-blob-into-a-container.
The sas url seems like https://myaccount.blob.core.windows.net/mycontainer/myblob?comp=blocklist&...
Here is the code as example.
URL sasUrl = new URL("<sas-url>");
try
{.
CloudBlockBlob blob = new CloudBlockBlob(sasUrl)
File source = new File(filePath);
blob.upload(new FileInputStream(source), source.length());
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
As reference, please see the javadocs for Azure Java Storage SDK.
Related
scrape one by one image text from a folder.this one for only one image.how to do for all the images
public static String crackImage(String filepath) throws TesseractException {
File imgfile = new File(filepath);
ITesseract instance = new Tesseract();
instance.setDatapath("C:\\selenium_work\\ScrapingText.PDF\\tessdata");
String result = instance.doOCR(imgfile);
return result;
}
public static void main(String[] args) throws TesseractException {
String textdata = ImageScraping.crackImage("C:\\selenium_work\\ScrapingText.PDF\\image\\IMG_20190305_152800__01.jpg");
System.out.println(textdata);
You can save the images from the given URL like:
Locate all <img> tags and extract their src attributes using i.e. XPath locator of //img
List<String> imagesUrls = driver.findElements(By.xpath("//img"))
.stream()
.map(img -> img.getAttribute("src"))
.collect(Collectors.toList());
Download all the images into the folder of your choice using OkHTTP client library:
for (String imageUrl : imagesUrls) {
Request request = new Request.Builder().url(imageUrl).build();
Response response = client.newCall(request).execute();
File downloadedLogo = new File("C:\\selenium_work\\ScrapingText.PDF\\image\\" + imageUrl.substring(imageUrl.lastIndexOf('/') + 1));
BufferedSink sink = Okio.buffer(Okio.sink(downloadedLogo));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();
}
Once done you should be able to call your crackImage function for each downloaded file via Files.walk() method:
Files.walk(Paths.get("C:\\selenium_work\\ScrapingText.PDF\\image"))
.forEach(file -> System.out.println(crackImage(file.toAbsolutePath().toString())));
How to write java code for egnyte chunked upload and send to rest service of egnyte api.
https://developers.egnyte.com/docs/read/File_System_Management_API_Documentation#Chunked-Upload
long size = f.getTotalSpace();
int sizeOfFiles = 1024 * 1024;// 1MB
byte[] buffer = new byte[sizeOfFiles];
ResponseEntity<String> responseEntity = null;
String fileName = f.getName();
String url = DOWNLOAD_OR_UPLOAD + "-chunked" + egnyteSourcePath + f.getName();
HttpHeaders headers = buildEgnyteEntity();
HttpEntity entity = new HttpEntity<>(headers);
//try-with-resources to ensure closing stream
try (FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis)) {
int bytesAmount = 0;
while ((bytesAmount = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
String filePartName = String.format("%s.%03d", fileName, partCounter++);
File newFile = new File(f.getParent(), filePartName);
responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
}
}
return responseEntity;
I think there's a couple of things missing in your code.
First thing is that you don't specify required headers. You should provide X-Egnyte-Chunk-Num with int value with number of your chunk, starting from 1. In X-Egnyte-Chunk-Sha512-Checksum header you should provide SHA512 checksum.
Second thing is that first request will give you an UploadId in response header in X-Egnyte-Upload-Id. You need to specify that as a header in your second and following requests.
Third thing is that I don't see you use your bytesAmount in the request. I'm not sure you're providing the data.
I'm not a Java guy, more of a C# one, but I've written a post how to upload and download big files with Egnyte API on my blog: http://www.michalbialecki.com/2018/02/11/sending-receiving-big-files-using-egnyte-api-nuget-package/. This can give you an idea how sending loop can be structured.
Hope that helps.
How to get a DataPart from MultipartFormData? I could not find any API to get that.
Http.MultipartFormData formData = body.asMultipartFormData();
// simple form field
// there is NO getData() or something available
DataPart imageIdPart = formData.getData("dataKey");
// uploaded file
FilePart imagePart = formData.getFile("imageKey");
I'm not used to work with Java in Play 2.0, but is something like that working ?
#BodyParser.Of(BodyParser.MultipartFormData.class)
public static Result index() {
Http.MultipartFormData multipartFormData = request().body().asMultipartFormData();
//ask the multipart to be form url encoded...
//and get the data
String[] data = multipartFormData.asFormUrlEncoded().get("dataKey");
//which should not impact such call
Http.MultipartFormData.FilePart image = multipartFormData.getFile("imageKey");
return ok("Got image: " + image.getFilename());
}
In scala:
def index = Action(parse.multipartFormData) {
request => {
val dataKey = request.body.dataParts.get("dataKey")
something something...
Ok("Ok!")
}
I'm using post and get request from Qualtrics api. They have sample code in java but I have a hard time to convert it into .net environment. The difficulity part is the download exported file(zip file). My question is how to implement the httpClient.execute in C#? I think in java it could hold the object without download into your physical drive and unzip the file....
Their sample code in java:
HttpGet statusGet = new HttpGet(fileUrl);
statusGet.setHeader("X-API-TOKEN", API_TOKEN);
HttpResponse response = httpClient.execute(statusGet);
// Extract exported file
ZipInputStream zs = new ZipInputStream(response.getEntity().getContent());
ZipEntry entry;
while ((entry = zs.getNextEntry()) != null)
{
FileOutputStream out = new FileOutputStream("./" + entry.getName());
IOUtils.copy(zs, out);
out.close();
}
}
Here's mine in c#:
using (var clientgetfile = new HttpClient())
{
clientgetfile.DefaultRequestHeaders.Accept.Clear();
clientgetfile.DefaultRequestHeaders.Add("X-API-TOKEN", "mytoken");
clientgetfile.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/zip"));
var response = clientgetfile.GetAsync(fileUrl);
DownloadFile = response.Result;
var byteArray = DownloadFile.Content.ReadAsStreamAsync().Result;
....
I'm retrieving a PDF file from a web server java, returning a byte array.
Need save the PDF on the local machine using C #, but the file is saved completely in blank, I think it is because of the byte array format is different.
Here is my code:
StreamReader responseReader = new StreamReader(webStream);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/pdf";
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
responseReader.Close();
byte[] docByte = Encoding.ASCII.GetBytes(response);
File.WriteAllBytes(#"C:\file.pdf", docByte);
Any suggestions on how to save the PDF file normally?
Thank you for listening
// ...
Stream webStream = webResponse.GetResponseStream();
using (var stream = File.Create(#"C:\file.pdf"))
{
webStream.CopyTo(stream);
}
Why don't you do it simply with WebClient like this?
using System.Net;
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(URL, #"C:\file.pdf");
}