Java ImageIO: can't read input file - java

I don't know why this isn't working, but the program says it can't read the input file. This is also being run in Ubuntu, by the way:
Here is the sample code:
URI url = new URI("images/GUI/TitleScreen.PNG");
File file = new File(url.toString());
bg = new ImageBackground(ImageIO.read(file));
The directory is located in the bin folder and src folder of the program as well.

What if you instead got your image as a stream from a resource? e.g.,
String imgPath = "images/GUI/TitleScreen.PNG";
BufferedImage buffImage = ImageIO.read(getClass().getResourceAsStream(imgPath));
bg = new ImageBackground(buffImage);

Related

Apache poi java: Image not showing in document when running JAR file in another computer

I created an app that works with files and works perfectly in the computer where I wrote the code, however it does not work correctly in other computers. After reviewing the entire code I was able to find the issue. I removed the following method that adds an image and the jar file works fine in multiple computers, however I need the image to be added. The method is the following: (doc is a static variable declared in another class, in case you wonder)
public void AddImage() throws IOException, InvalidFormatException {
XWPFParagraph parag = document.createParagraph();
XWPFRun r = parag.createRun();
URL imageURL = ClassLoader.getSystemResource("TheImage.png");
String imageName = imageURL.getPath();
File image = new File(imageName);
FileInputStream fis = new FileInputStream(image);
BufferedImage bimg1 = ImageIO.read(image);
int width = 160;//bimg1.getWidth();
int height = 26;//bimg1.getHeight();
String imgFile = image.getName();
r.addPicture(fis, document.PICTURE_TYPE_PNG, imgFile, Units.toEMU(width), Units.toEMU(height));
}
To give you more details, I created a source file in the project in eclipse where I added the image, does anybody know how to solve this?
Perhaps on your computer the image is taken from the file system instead from the jar. Is the image packaged in the jar file? Then try
Inputstream logo = getClass().getResourceAsStream("/path/in/jar/img.png");
to load it.
How to read a file from jar in Java?

how to upload file to Box subfolder using Java SDK

How to upload files to my Box Sub-Folder using either by subfolder name or ID
Example say I have 2 subfolders(subfolder1 and subfolder2) in my Box, How to upload files to subfolder2 using java sdk.
Can we upload using any new methods.
Successful in uploading files to Box root folder using the code below
BoxFolder bfolder = BoxFolder.getRootFolder(api);
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
bfolder.uploadFile(stream, fileName);
You probably need to enumerate the folders till you find subfolder1, then create a new BoxFolder from that. Something like this (edit for compile errors):
BoxFolder bfolder = BoxFolder.getRootFolder(api);
Iterator<BoxFolder.Info> it = bfolder.getChildren().iterator();
for(BoxFolder.Info i : it){
if(i.getName().equals(subfolder1)){
BoxFolder folder = new BoxFolder(api, i.getID());
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
folder.uploadFile(stream, fileName);
break;
}
}

Image in loading from specified path

When I am giving absolute path of image means from my project scr directory it loads the image but when I am trying to load image from specified path or relative path then it shows null error, it doesn't read image.
i have get path using JFilechooser directory specifically, it only display .jpg image and give list of files to File type and stored one by one image in string variable and then want to load image using that variable.
My code:
final String fpath;
final File dir = new File("");
final JFileChooser file;
file = new JFileChooser();
file.setCurrentDirectory(dir);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
file.showOpenDialog(contentPane);
final File[] lofFile = file.getCurrentDirectory().listFiles();
for (int i = 0; i < a; i++) {
if (lofFile[i].toString().substring(lofFile[i].toString().lastIndexOf(".") + 1).equals("jpg")) {
az = lofFile[i].toString();
}
}
private BufferedImage bg;
bg = ImageIO.read(getClass().getResource(az));
Throws this exception:
java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)
Any help please?
Class#getResource is not for loading files from the file system. Maybe ImageIO.read(az) will work.
But you get a File object from the JFileChooser. Use it and you have no problem at all. Don't do any String manipulation on filenames if there is no reason for it. Filtering for a specific suffix is not a reason.

AesZipFileEncrypter zipAndEncrypt method adds all folder tree to file

I'm using this method to zip and decrypt a file:
AesZipFileEncrypter.zipAndEncrypt
This code:
AesZipFileEncrypter.zipAndEncrypt(new File("C:\Test\Folder\MyFile.txt"), new File("C:\Test\Folder\MyZip.zip"), password, aesEncrypter);
compresses also the folder tree of my file, not just the file. For example:
Adding C:\Test\Folder\MyFile.txt in the created zip file I will find the folders C:\Test\Folder\MyFile.txt also if I would like to have just MyFile.txt in the root folder.
Is it possibile?
This is the solution:
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(password, 0);
AesZipFileEncrypter ze=new AesZipFileEncrypter(outputfilename, aesEncrypter);
ze.add(inputfilename,new FileInputStream(inputfilename), password);
ze.close();
In Windows (which it looks like you are in) I ran into the same problem and it seems to depend on where the file is relative to your application. To get around it, I copied the input file to the local directory, ziped and encrypted it, then moved the output file back to the where the output file was intended.
public static File aesEncrypt(String inFileFullPath, String outFileFullPath, String aesPassword) throws IOException{
File inFile = new File(inFileFullPath);
File localInput = new File(inFile.getName());
Files.copy(inFile, localInput);
File outFile = new File(outFileFullPath);
File localOutFile = new File(outFile.getName());
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(aesPassword, 255);
AesZipFileEncrypter ze = new AesZipFileEncrypter(localOutFile, aesEncrypter);
ze.add(localInput, aesPassword);
ze.close();
Files.move(localOutFile, outFile);
localInput.delete();
return outFile;
}

Delete a File given an url using java

URL urlImage = new URL(candidateImagePath);
BufferedImage image = ImageIO.read(urlImage);
String imageName = urlImage.getFile().split("/")[3];
String pathImage = messageSource.getMessage("consultant.image", null, Locale.ENGLISH)+messageSource.getMessage("system.slash", null, Locale.ENGLISH)+candidateid;
File fileDir = new File(pathImage);
fileDir.mkdirs();
ImageIO.write(image, "jpg" ,new File(pathImage+messageSource.getMessage("system.slash", null, Locale.ENGLISH)+imageName));
I am trying to get an image uploaded by iphone guys to a temporary url .
I read the image and write it to my desired location which is 'pathImage'.
Till here everything works fine.
I want to delete the temporary file in the url.
I want to know how can I delete the image when the url is given in java. Kindly help on this .
You need to write a wrapper on target URL, which will simply delete the file passed as a query string like
http://your_temp_url/deleteimage?file=your_temp_file_name
in deleteimage you will code like this
private void deletefile(String file)
{
File f1 = new File(file);
boolean success = f1.delete();
if (!success){
out.println("Deletion failed.");
}else{
out.println("File deleted.");
}
This is just one approach to delete the image located on remote machine.
new File(url.toURI()).delete();
should do the trick.
example:
URL url=getClass().getResource("/someresources/resourceFile.txt");
File f=new File(url.toURI());
f.delete();

Categories

Resources