Get name of an image in java - java

Is it possible to get the name of an image in java. The image source is a url.
For eg. "http://172.16.2.42/apache_pb.png"
I need the output "apache_pb.png"

String s = "http://172.16.2.42/apache_pb.png";
int index = s.lastIndexOf('/');
String name = s.substring(index+1);
System.out.println(name);

You can use this helper method from the URL class:
String file = new URL("http://172.16.2.42/apache_pb.png").getPath();

Would URL#getFile() do this for you?

Related

How to return an Image to browser in rest API in JAVA from folder?

I have a folder in my NetBeans project with images. I have a H2 database with a "properties" table.
The images are named after a column in the properties table for convenience. This is my code so far.
#PostMapping(value = "/image/large/{id}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<Image> getPicture(#PathVariable long id)throws Exception{
System.out.println(id);
//System.out.println(barcode);
Properties prop1 = new Properties();
prop1 = propService.findOne(id);
String filepath = prop1.getPhoto();
String img = "static/images/"+filepath;
return img;
}
How can I implement this in my rest controller? Struggling to find a correct way to implement this, any help appreciated.
From the code you have provided, you can return a string representing an image location or path. This path can then be used in am image <img /> tag.
The second option is to read your file using an inputstream and convert the image to base46 (which you then return to the client).

Adding variables to file path in java

Is it possible to modify a file path so that it incorporates a String variable?
example code
String var = "Picture.jpg";
ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\var");
Use String.format
String var = "Picture.jpg";
ImageIcon img = new ImageIcon(String.format("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\%s", var));

How to get access to object

I am new to java.
I have a directory with a txt file (R.raw). I want to get access with a command
InputStream in_s = res.openRawResource(R.raw.itemname);
where itemname is a dynamic string with a filename from a previous activity.
How do I can get open file in R.raw by a string "n0.txt"?
In javascript I can implement it like R.raw["n0.txt"] or R.raw.itemname.
Thanks in advance.
You need to use method getIdentifier().
Context context;
Resources res;
int itemId = res.getIdentifier("itemname", "raw", context.getPackageName());
InputStream is = res.openRawResource(itemId);
Original post here.

Textscreen in Codename One, how to read text file?

I want to add a help screen to my Codename One App.
As the text is longer as other strings, I would like put it in a separate file and add it to the app-package.
How do I do this? Where do I put the text file, and how can I easily read it in one go into a string?
(I already know how to put the string into a text area inside a form)
In the Codename One Designer go to the data section and add a file.
You can just add the text there and fetch it using myResFile.getData("name");.
You can also store the file within the src directory and get it using Display.getInstance().getResourceAsStream("/filename.txt");
I prefer to have the text file in the filesystem instead of the resource editor, because I can just edit the text with the IDE. The method getResourceAsStream is the first part of the solution. The second part is to load the text in one go. There was no support for this in J2ME, you needed to read, handle buffers etc. yourself. Fortunately there is a utility method in codename one. So my working method now looks like this:
final String HelpTextFile = "/helptext.txt";
...
InputStream in = Display.getInstance().getResourceAsStream(
Form.class, HelpTextFile);
if (in != null){
try {
text = com.codename1.io.Util.readToString(in);
in.close();
} catch (IOException ex) {
System.out.println(ex);
text = "Read Error";
}
}
The following code worked for me.
//Gets a file system storage instance
FileSystemStorage inst = FileSystemStorage.getInstance();
//Gets CN1 home`
final String homePath = inst.getAppHomePath();
final char sep = inst.getFileSystemSeparator();
// Getting input stream of the file
InputStream is = inst.openInputStream(homePath + sep + "MyText.txt");
// CN1 Util class, readInputStream() returns byte array
byte[] b = Util.readInputStream(is);
String myString = new String(b);

how to convert byte array to image in java?

I am developing a Web application in Java. In that application, I have created webservices in Java. In that webservice, I have created one webmethod which returns the image list in base64 format. The return type of the method is Vector. In webservice tester I can see the SOAP response as xsi:type="xs:base64Binary". Then I called this webmethod in my application. I used the following code:
SBTSWebService webService = null;
List imageArray = null;
List imageList = null;
webService = new SBTSWebService();
imageArray = webService.getSBTSWebPort().getAddvertisementImage();
Iterator itr = imageArray.iterator();
while(itr.hasNext())
{
String img = (String)itr.next();
byte[] bytearray = Base64.decode(img);
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
imageList.add(imag);
}
In this code I am receiving the error:
java.lang.ClassCastException: [B
cannot be cast to java.lang.String" on
line String img = (String)itr.next();
Is there any mistake in my code? Or is there any other way to bring the image in actual format? Can you provide me the code or link through which I can resolve the above issue?
Note:- I already droped this question and I got the suggetion to try the following code
Object next = iter.next();
System.out.println(next.getClass())
I tried this code and got the output as byte[] from webservice. but I am not able to convert this byte array to actual image.
is there any other way to bring the image in actual format? Can you provide me the code or link through which I can resolve the above issue?
You can check this link which provides information about converting image to Byte[] and Byte[] back to image. Hope this helps you.
http://www.programcreek.com/2009/02/java-convert-image-to-byte-array-convert-byte-array-to-image/
To convert use Base64.decode;
String base64String = (String)itr.next();
byte[] bytearray = Base64.decode(base64String);
BufferedImage imag=ImageIO.read(bytearray);
I'm not familiar with what you're trying to do, but I can say this: String does have a constructor that takes a byte[].
If I understood you correctly, you tried to do String s = (String) byteArray;, which of course doesn't work. You can try String s = new String(byteArray);.
Looking at the actual error message:
java.lang.ClassCastException: [B cannot be cast to java.lang.String
on line String img = (String)itr.next();
I'm saying that perhaps you meant to do:
String img = new String(itr.next());

Categories