I have just such a problem, the following small code image downloader where params[0]- url and params[2] - file name, this design works well on Android 5.0+, but on older versions, I get an error:
-javax.net.ssl.SSLException: Connection closed by peer
#Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
InputStream is = null;
try {
is = new URL(params[0]).openStream();
} catch (IOException e) {
e.printStackTrace(); // Exception there
}
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream outStream = null;
File file = new File(Constans.DATA_APPLICATION_PATH+"/photo/"+params[1].replace("\"","").trim());
try {
// make a new bitmap from file
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
file.deleteOnExit();
return bitmap;
}
`
You have problem with SSL authorization, which will come for sure if you are using self signed certificates.
So the way to make android app to connect to URL is to skip the SSL verification (Only if we are sure about the connection)
For this you can add this class and execute it inside onCreate method
new NukeSSLCerts().nuke();
It will skip certificate verification by trusting all certificates.
Related
I'm trying to upload my object to s3 bucket, but no response is getting back from client builder.
CompletableFuture.runAsync(()->{
try {
//runs up to here
System.out.println("start connection");
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(AppConstants.AWS_REGION).build();
System.out.println("connected");
//nothing executed below!
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(mimeType);
metadata.addUserMetadata("x-amz-meta-title", "someTitle");
metadata.setContentLength(bytes.length);
PutObjectRequest request = new PutObjectRequest(bucket, fileName, bais , metadata);
request.setCannedAcl(CannedAccessControlList.BucketOwnerFullControl);
s3.putObject(request);
}catch (AmazonServiceException e) {
//no errors logged
e.printStackTrace();
}catch (SdkClientException e) {
// no errors logged
e.printStackTrace();
}
catch(Exception e) {
//no errors logged
e.printStackTrace();
}
});
The server is logging the following:
(ForkJoinPool.commonPool-worker-5) start connection
and then nothing happens
PS: Libraries I'm using:
aws-java-sdk-core-1.11.482
aws-java-sdk-s3-1.11.482
I also have my credential file in ~/.aws/credentials
The aws-java-sdk internally uses joda-time library which was missing and not connecting, I have included it and it worked.
I'm having some trouble with transferring files to my ftp.
with this code i get no errors, but I also don't get a file to my ftp server. I hope some more experienced users can point me to the right direction.
FTPClient client = new FTPClient();
FileInputStream picture = null;
try {
client.connect("adress" ,port);
client.login("user", "password");
client.changeWorkingDirectory("/htdocs/javaprojekt");
client.setFileType(FTP.BINARY_FILE_TYPE);
String filename = "/Users/sicknk/Documents/Java Projekt/fyeah.jpg";
picture = new FileInputStream(filename);
client.storeFile(filename, picture);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (picture != null) {
picture.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I was trying to implement the functionality of transferring local files to a network drive using jcifs library but upon running the program on the command line I was receiving following exception:
Exception in thread "main" java.lang.NullPointerException
jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java: 202)
To understand the error I tried debugging the code on eclipse and while doing so at line:
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication("xxxxxx.xx.com",username,password);
I received an exception stating ClassNotFoundException. But I have the jcifs.jar in the build path.
A quick google search for 'ntlmpasswordauthentication' classnotfoundexception landed me on two threads with same issue but no resolution.
Please let me know how can I resolve this.
Thank you
Here is the whole function, just in case needed:
private static void TransferFiles()
{
File transfer_files = new File (sourcepath);
File[] files = transfer_files.listFiles();
String username = properties.getProperty("user");
String password = properties.getProperty("password");
String source = sourcepath;
SmbFileOutputStream outputStream = null;
FileInputStream inputStream = null;
SmbFile copyFile = null;
byte[] buffer = new byte[16*1024*1024];
int length = 0;
jcifs.smb.NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication("xxxxxxx",username,password);
try
{
copyFile = new SmbFile(destinationpath,authentication);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
outputStream = new SmbFileOutputStream(copyFile);
}
catch (SmbException | MalformedURLException | UnknownHostException e)
{
e.printStackTrace();
}
try
{
inputStream = new FileInputStream(sourceFile);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
try
{
while((length = inputStream.read(buffer))>0)
{
outputStream.write(buffer, 0, length);
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close();
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
So I'm uploading a file to my VPS (Linux Centos 5 64 bit) via FTP using Java. The code I'm using to upload to my VPS is
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
Now the code is working but what I want is to change where on the VPS it uploads the file to. Right now it's
serverip/HERE
I have some files so want to change it to
serverip/file/HERE
How can I go about doing so?
You can use the changeCurrentWorkingDirectory() method to change to the desired directory. Once you're in there, you can write the file using storeFile just like before.
changeCurrentWorkingDirectory returns true if the directory change was successful, otherwise it returns false. It takes a string which interpreted as the directory path. If the path starts with a slash, it's interpreted as absolute path starting at the ftproot directory. Otherwise it's interpreted as relative path.
Revised code could look something like this:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// change directory to serverip/file/
if (client.changeWorkingDirectory("/file"))
{
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
}
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
I am trying to download image from server. Few are downloading and few and creating problem. I don't know why.
I have downloaded and show image to user on the same location. Here is the file which is able to download.
http://www.mongreldog.co.nz/unilogo/Backgrounds_20399.png
When I am trying to download following image. This image is opening in browser but not downloading in android
http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester%20United_M_785.jpg
Its give exception
java.io.FileNotFoundException: http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester United_M_785.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
at src.com.mongreldog.appsupport.Utils.downloadImage(Utils.java:77)
at src.com.mongreldog.ViewFullCompAndCommentActivity$3.performInBackground(ViewFullCompAndCommentActivity.java:607)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:44)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
Here is my code.
public static Bitmap downloadImage(String imageURLStr) {
Bitmap bitmap = null;
InputStream in = null;
try {
URL url = new URL(imageURLStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (SocketTimeoutException e) {
bitmap = null;
e.printStackTrace();
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
bitmap = null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
bitmap = null;
}
return bitmap;
}
Use URL Encoder to encode the image URL, as you can see the URL have white spaces in the log report.
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = URLEncoder.encode(imageURLStr, "utf-8");
//... rest of your code.
}
Edit: as you reported of issue '+' instead of %20
you can use
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = imageURLStr.replaceAll(" ", "%20");
//... rest of your code.
}
For source check here
I try to use URLEncoder.encode() to encode the URL. Its strange that it convert " " with "+".
Please try Uri.encode(imageURL). I just try it and its working perfectly.
I have tested that in android.
Looks like the file name is not being parsed correctly (the space between 'Manchester' and 'United').
Use URLEncoder.encode() to encode the URL.
Your second URL is very instable. It may return 404 in the most cases even in Chome. I have seen the picture only once.