My issue: my android device is unable to connect to a public FTP server using URLConnection. However, when the device tried to connect to a public FTP server (anonymous for username and no password), it cannot retrieve the InputStream from urlConnection.getInputStream();.
I do not use FTPClient from Apache Commons library because the speed is quite limited... A related question regarding that limited speed with FTPClient is here
FTPClient's download is slower than URLConnection's download.
Whatever I tried with the urlString:
urlString = protocol+"://" + username+"#" +address+":"+port + filepath + file;
// ftp://anonymous#speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + username+":" + password + "#" +address+":"+port + filepath + file;
// ftp://anonymous:#speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + username+":" + "password" + "#" +address+":"+port + filepath + file;
// ftp://anonymous:password#speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + address+":"+port + filepath + file;
// ftp://speedtest.tele2.net:21/100GB.zip
downloadAndBroadcastURL:after getinputStream is never got logged with open FTP servers. I also tried with these two public FTP servers:
ftp://speedtest.tele2.net/100GB.zip
ftp://ftp.funet.fi/dev/1GBnull
But it doesn't connect as well.
My code:
private void downloadAndBroadcastURL() {
beginTime = System.currentTimeMillis();
try {
String urlString;
if (username.equals("anonymous")){
urlString = protocol+"://" + username+"#" +address+":"+port + filepath + file;
} else {
urlString = protocol+"://" + username+":" + password + "#" +address+":"+port + filepath + file;
}
Log.d(TAG, "downloadAndBroadcastURL: "+ urlString);
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
Log.d(TAG, "downloadAndBroadcastURL:after connect ");
inputStream = new BufferedInputStream(urlConnection.getInputStream());
Log.d(TAG, "downloadAndBroadcastURL:after getinputStream");
/*******************************
The rest is about handling the input stream, the download...
*****************************/
long difference;
byte data[] = new byte[4094];
int count;
while ((count = inputStream.read(data)) != -1 && download) {
downloadCount += count;
long stopTime = System.currentTimeMillis();
difference = stopTime - beginTime;
if (difference > 1000 && download) {
currentSpeed = downloadCount / (difference / 1000L); //for precision, because difference can be more than 1000 sometimes
averageSpeed = (averageSpeed + currentSpeed) / 2;
Log.d(TAG, "Speed: " + downloadCount + " bytes/s. " + (downloadCount / 1024) + "kB/s " + (downloadCount / 128) + "kbit/s " + (downloadCount / 1048576) + "Mb/s " + ((downloadCount) / (1024 * 128)) + "Mpbs" + (downloadCount / 131072) + "Mbit/s difference:" + difference + "ms");
if (download) {
broadcastSpeed();
}
downloadCount = 0; //Resetoidaan määrä
beginTime = stopTime;
}
if (!download) {
Log.d(TAG, "downloadAndBroadcast: download stops");
clearInputStream();
}
}
clearInputStream();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "FAIL " + e.toString());
} finally {
Log.d(TAG, "downloadAndBroadcastURL: ");
clearInputStream();
Log.d(TAG, "downloadAndBroadcast: finally");
}
}
Does anyone have any idea? Thanks in advance!
Related
i want to split pdf to image file by page, but i got Warning: You did not close a PDF Document looping when renderImageWithDPI
Still have warning
UPDATE CODE :
public void splitImage(PDDocument document, File checkFile, File theDirSplit, String fileExtension, File theDir, File watermarkDirectory, int numberOfPages)
throws InvalidPasswordException, IOException {
String fileName = checkFile.getName().replace(".pdf", "");
int dpi = 300;
if (theDirSplit.list().length < numberOfPages)
{
for (int i = 0; i < numberOfPages; ++i)
{
if (i == numberOfPages)
break;
if (theDirSplit.list().length != numberOfPages)
{
File outPutFile = new File(theDirSplit + Constan.simbol + fileName + "_" + (i + 1) + "." + fileExtension);
document = PDDocument.load(checkFile);
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage bImage = pdfRenderer.renderImageWithDPI(i, dpi, ImageType.RGB);
ImageIO.write(bImage, fileExtension, outPutFile);
}
// splitService.watermark(outPutFile, (i + 1), watermarkDirectory, "pdf");
}
document.close();
//System.out.println("Converted Images are saved at -> " + theDirSplit.getAbsolutePath());
}
System.out.println("Done Partial SPlit");
/*
* int i = 1; while (iterator.hasNext()) { PDDocument pd = iterator.next();
* pd.save(theDirSplit + Constan.simbol + i++ + ".pdf"); }
* System.out.println("Multiple PDF’s created");
*/
}
error looping
total warning same with number of pages...
i already try to close but not work, this process make my server java.lang.OutOfMemoryError: Java heap space
update :
else if ("pdf".equalsIgnoreCase(typeFile)) {
System.out.println(
"target file " + downloadPath + R_OBJECT_ID + Constan.simbol + R_OBJECT_ID + "." + typeFile);
//get jumlah halaman
try(PDDocument document = PDDocument.load(checkFile)){
File theDirSplit = new File(theDir.getAbsolutePath() + Constan.simbol + "splitImage");
createFolder(theDirSplit);
String fileExtension = "jpeg";
File watermarkDirectory = new File(theDir.getAbsolutePath() + Constan.simbol + "watermarkImage");
createFolder(watermarkDirectory);
// split 2 page image
if (theDirSplit.list().length <= document.getNumberOfPages()) {
try {
splitImage(document,checkFile, theDirSplit, fileExtension, theDir, watermarkDirectory, document.getNumberOfPages()/2);
} catch (IOException e) {
System.out.println("ERROR SPLIT PDF " + e.getMessage());
e.printStackTrace();
}
}
res.setTotalPages(document.getNumberOfPages());
document.close();
return new ResponseEntity<>(res, HttpStatus.OK);
}
} else {
res.setTotalPages(1);
return new ResponseEntity<>(res, HttpStatus.OK);
}
this is code to call split method....
This is somewhat lost from the question, but the cause was failing to close the documents generated by splitter.split().
Below is the code which we are using to call Microsoft Dynamics. I want to know how many parallel connection this code can create or how many parallel request it can send.
HttpClient client = new HttpClient();
client.getParams().setParameter(CredentialsProvider.PROVIDER, new ConsoleAuthPrompter(logger));
String strURL = getstringURL();
String strSoapAction = "";
strSoapAction = getSoapAction();
PostMethod httppost = new PostMethod(strURL);
String ContetType = getContentType();
RequestEntity entity = new StringRequestEntity(getSOAPxml(),"application/json", "utf-8");
httppost.setRequestEntity(entity);
httppost.setRequestHeader("SOAPAction", strSoapAction);
httppost.setDoAuthentication(true);
try {
// execute the GET
int status = client.executeMethod(httppost);
if (httppost.getStatusCode() == 200)
setReply_SOAPxml(httppost.getResponseBodyAsString());
else {
logger.error("Server Replied with Error :"
+ httppost.getStatusCode() + " "
+ httppost.getStatusText() + " "
+ httppost.getResponseBodyAsString());
throw new Exception("Server Replied with Error : ["
+ httppost.getStatusLine().toString() + "] "
+ httppost.getStatusCode() + " "
+ httppost.getStatusText() + " "
+ httppost.getResponseBodyAsString());
}
} finally {
// release any connection resources used by the method
httppost.releaseConnection();
}
}
Does anyone know how to see the progress (in percent) of an upload in a multipart upload in Amazon S3?
I would do it like this:
MultipleFileUpload transfer = transferManager.uploadDirectory(mybucket, null, new File(localSourceDataFilesPath), false);
// blocks the thread until the upload is completed
showTransferProgress(transfer);
Then in showTransferProgress, I would create a block the upload using a sleep, and do the math every X seconds:
private void showTransferProgress(MultipleFileUpload xfer) {
while (!xfer.isDone()) {
// some logic to wait so you don't do the math every second like a Thread.sleep
TransferProgress progress = xfer.getProgress();
long bytesTransferred = progress.getBytesTransferred();
long total = progress.getTotalBytesToTransfer();
Double percentDone = progress.getPercentTransferred();
LOG.debug("S3 xml upload progress...{}%", percentDone.intValue());
LOG.debug("{} bytes transferred to S3 out of {}", bytesTransferred, total);
}
// print the final state of the transfer.
TransferState xferState = xfer.getState();
LOG.debug("Final transfer state: " + xferState);
}
this line is what you are looking for:
Double percentDone = progress.getPercentTransferred();
Hi guys here is is my final version
private void awsHoldUntilCompletedAndShowTransferProgress(Upload upload) throws InterruptedException, AmazonClientException {
TransferProgress tProgress = upload.getProgress();
long totalSize = tProgress.getTotalBytesToTransfer();
long bPrevious = 0;
int timerSec = Math.toIntExact(Math.round(Math.sqrt(totalSize / 1024 / 1024) / 4));// calculate based on file size
if (timerSec > 60) {// no longer than 60 second per loop
timerSec = 60;
}
while (!upload.isDone()) {
long bTransferred = tProgress.getBytesTransferred();
String strMbps = Double.valueOf((((bTransferred - bPrevious) / timerSec) / 1024) / 1024).toString() + " MBps";
String strTransfered = bTransferred + " bytes";
if (bTransferred > 1024) {
strTransfered = Double.valueOf((bTransferred / 1024) / 1024).toString() + " MB";
}
log.info("Upload progress: "
+ strTransfered
+ " / "
+ FileUtils.byteCountToDisplaySize(totalSize) + " - "
+ Math.round(tProgress.getPercentTransferred()) + "% "
+ strMbps);
bPrevious = bTransferred;
TimeUnit.SECONDS.sleep(timerSec);
}
Transfer.TransferState transferState = upload.getState();
log.info("Final transfer state: " + transferState);
if (transferState == Transfer.TransferState.Failed) {
throw upload.waitForException();
}
}
and here is where I call the code above from
..stuff...
TransferManager tm = TransferManagerBuilder
.standard()
.withS3Client(s3Client)
.build();
LocalDateTime uploadStartedAt = LocalDateTime.now();
log.info("Starting to upload " + FileUtils.byteCountToDisplaySize(fileSize));
Upload up = tm.upload(bucketName, file.getName(), file);
awsHoldUntilCompletedAndShowTransferProgress(up);
log.info("Time consumed: " + DurationFormatUtils.formatDuration(Duration.between(uploadStartedAt, LocalDateTime.now()).toMillis(), "dd HH:mm:ss"));
this works, but may be a better way to do this
TransferManager tm = TransferManagerBuilder
.standard()
.withS3Client(s3Client)
.build();
PutObjectRequest request = new PutObjectRequest(bucketName, file.getName(), file);
ProgressListener progressListener = new ProgressListener() {
#Override
public void progressChanged(com.amazonaws.event.ProgressEvent progressEvent) {
bytesUploaded += progressEvent.getBytesTransferred();// add counter
if (bytesUploaded > byteTrigger) {
if ((bytesUploaded + sizeRatio) < fileSize) {
byteTrigger = bytesUploaded + sizeRatio ;
} else {
byteTrigger = bytesUploaded + (sizeRatio / 6);// increase precision approaching the end
}
String percent = new DecimalFormat("###.##").format(bytesUploaded * 100.0 / fileSize);
log.info("Uploaded: " + FileUtils.byteCountToDisplaySize(bytesUploaded) + " - " + percent + "%");
}
}
};
request.setGeneralProgressListener(progressListener);
Upload upload = tm.upload(request);
log.info("starting upload");
upload.waitForUploadResult();
log.info("Upload completed");
Currently, My system send file from AS400 to Mainframe by FTP. The downstream Mainframe system will not support FTP any more. FTPS will be the only way to send files to it.
Currently we use com.ibm.as400.access.FTP and this package to execute FTP command. I want to know What package can be used by FTPS from AS400 to Mainframe.
currently ftp code is as follows:
boolean FTPFile(String filepath, String filename, String country) {
String ftpresult = "";
String region = bundle.getString("region." + country);
FTP ftp = new FTP(ftpHost, ftpId, ftpPw);
try {
if (country.equals("766") || country.equals("858")
|| country.equals("761") || country.equals("836")) {
ftp.setDataTransferType(FTP.BINARY);
}
if (ftp.connect()) {
getLogMsg().append("FTPing SIF file. Region = " + region,
LogMsg.TEXT_MSG);
String sendname = "'" + ftpDataset + country + ".LSIFATB'";
getLogMsg().append(
"sending file: = " + filepath + filename + " as "
+ sendname, LogMsg.TEXT_MSG);
// Korea
if (country.equals("766") || country.equals("761")) {
ftp.issueCommand("ltype c 933");
// ftpresult = ftp.issueCommand("type b 6");
}
// Taiwan
if (country.equals("858") || country.equals("836")) {
ftp.issueCommand("ltype c 937");
// ftpresult = ftp.issueCommand("type b 8");
}
boolean ftp_res = ftp.put(filepath + filename, sendname);
// System.out.println("ftp result: " + ftp_res);
getLogMsg().append("ftp result (sending SIF): " + ftp_res,
LogMsg.TEXT_MSG);
ftpresult = ftp.issueCommand("site filetype=jes");
System.out.println("ftp command result = " + ftpresult);
String jobname = bundle.getString("app.sif.jobname." + country);
// String jobname = "CISBL30D";
ftp.setDataTransferType(FTP.ASCII);
ftp_res = ftp.put(bundle.getString("app.sif.directory") + "/"
+ region + country + "/jcl.txt", "jcl.txt '" + jobname
+ "'");
getLogMsg().append("ftp result (sending jcl): " + ftp_res,
LogMsg.TEXT_MSG);
return true;
}
} catch(...) {}
}
I've used the Apache COMMONS Net module to do normal FTP.
It does have a FTPSClient class that should do what you want.
I have a application that runs as a schedule.It connect to ftp server and get files from remote folder.scheduler runs in every 5min time.Sometimes when there are lot of files in remote location, scheduler runs again while first cycle is running.In such situation some times it download 0 size files even actual file size is greater than 0 in remote location.Does anyone have any idea why this happen?
below is the code to import files.
private void importEDIFiles(String host, String user, String password, String path, String road) {
try {
String edi824Path = path + "/" + EDI_824_FOLDER;
FTPBroker ftpBroker = new FTPBroker(host, user, password, edi824Path);
FTPClient client = ftpBroker.makeFTPConeection();
String os = client.getSystemName();
client.setFileTransferMode(FTP.ASCII_FILE_TYPE);
File edi824File = null;
File edi824Filebak = null;
ArrayList<FTPFile> files;
try {
FTPFile[] ftpfiles = client.listFiles();
logger.info("\t" + ftpfiles.length + " files are in ftp location ");
if (ftpfiles.length > 0) {
files = removeZeroFiles(ftpfiles);
for(int x=0;x<files.size();x++){
logger.info("name ---"+files.get(x).getName());
logger.info("size ----"+files.get(x).getSize());
}
String ftpFile = null;
logger.info("\t" + files.size() + " downloading from " + road + " rail road.");
for (int i = 0; i < files.size(); i++) {
ftpFile = files.get(i).getName();
logger.info("\t" + ftpFile + " is downloading....");
// logger.info("\t" + ftpFile + " size ...." + ftpFile.isEmpty());
String source = destinationFilePath + pathSeparator + road + pathSeparator + ftpFile;
String target = edi_824backupFilePath + pathSeparator + road + pathSeparator + ftpFile;
edi824File = new File(source);
edi824Filebak = new File(target);
FileOutputStream fosout = new FileOutputStream(source);
boolean isRetrieved = client.retrieveFile(ftpFile, fosout);
logger.debug("isRetrieved : " + isRetrieved);
FileUtils.copyFile(edi824File,edi824Filebak);
fosout.flush();
fosout.close();
boolean isDelete = client.deleteFile(ftpFile);
logger.debug("isDelete : " + isDelete);
}
} else {
logger.info("No files to Pull in the FTP Location for " + user);
//throw new RuntimeException("No files to Pull in FTP Location.");
}
} catch (Exception e) {
logger.error(e,e);
e.printStackTrace();
} finally {
client.logout();
client.disconnect();
}
} catch (Exception ex) {
logger.error(ex, ex);
ex.printStackTrace();
}
}
you can use a flag boolean isRunning(), setRunning(boolean ), and synchronize your code so that two or more threads would not run the same method at the same time