today I was damaging my brain for too long on how I am able in Apache wicket to access the actual filecontent of a CssResourceReference?
The CSSResourceReference is declared like that:
private final CssResourceReference CSS_GLOBAL = new CssResourceReference(BasePage.class, "css/global.css");
...and is meant to be used for basic header contribution as well as it's contents need to be accessible from within a behaviour for feeding a LessCSSEngine.
I know that I am able to get the url (via the WicketWiki):
RequestCycle.get().urlFor(CSS_GLOBAL, null);
But from there on, I am stuck on how to actually access the file and contents.
Any help appreciated.
You can't access the content that easily because a ResourceReference in Wicket is meant to load by a Request. You can simulate a Request and read the response but this is too much work.
Anyways you don't need Wicket to access files in your class path:
final InputStream stream = BasePage.class.getResourceAsStream("css/global.css");
final Reader reader = new InputStreamReader(stream);
Related
I'm trying to read illustrator file metadata value by using Exiftool. I tried as per below.
File[] images = new File("filepath").listFiles();
ExifTool tool = new ExifTool(Feature.STAY_OPEN);
for(File f : images) {
if (f.toString().contains(".ai"))
{
System.out.println("test "+tool.getImageMeta(f, Tag.DATE_TIME_ORIGINAL));
}
}
tool.close();
Above code not printing any value. I even tried this.
public static final File[] IMAGES = new File("filepath").listFiles();
ExifTool tool = new ExifTool(Feature.STAY_OPEN);
for (File f : IMAGES) {
System.out.println("\n[" + f.getName() + "]");
System.out.println(tool.getImageMeta(f, Format.NUMERIC,
Tag.values()));
}
Which only prints {IMAGE_HEIGHT=2245, IMAGE_WIDTH=5393}. How do I call metadata values using Exiftool. Any advices and references links are highly appreciated.
For the given API, it either;
1-does not contain the tag you are looking for
2-the file itself might not have that tag filled
3-you might want to recreate your own using a more general tag command when calling exiftool.exe
Look in the source code and find the enum containing all the tags available to the API, that'll show you what you're restricted to. But yeah, you might want to consider making your own class similar to the one you're using. I'm in the midst of doing the same. That way you can store the tags in perhaps a set or HashMap instead of an enum and therefore be much less limited in tag choice. Then, all you have to do is write the commands for the tags you want to the process's OutputStream and then read the results from the InputStream.
I have a JEditorPane in my application and I was loading java files into it using jsyntaxpane and the following code and it was working perfectly:
to highlight
jsyntaxpane.DefaultSyntaxKit.initKit();
textarea.setContentType("text/java");
to load file in
int a = filesToCompileList.getSelectedIndex();
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
textarea.read.read(br, index);
br.close();
textarea.requestFocus();
but know i had to change the way i was loading the file in and i am currently loading the files in like
File file = new File(filePath);
textarea.setPage(file.toURI().toURL());
this is loading the files in the way i want but isn't highlighting the text for java files anymore! does anybody know how i can fix this or get java highlighting a different way?
AFAIK, it can not be done if you don't implement your own version of the jeditorpane. From the javadoc
The setPage method can be used to initialize the component from a URL.
In this case, the content type will be determined from the URL, and
the registered EditorKit for that content type will be set.
So, the mime type of the content will be inherit from the mime type of the url. Call setContentyType later will have no effect, as this will change the model of the jeditorpane, cleaning the content. Again from the Javadoc
NOTE: This has the side effect of changing the model, because the
EditorKit is the source of how a particular type of content is
modeled. This method will cause setDocument to be called on behalf of
the caller to ensure integrity of the internal state.
So you must keep using the read method.
A webpage contains a link to an executable (i.e. If we click on the link, the browser will download the file on your local machine).
Is there any way to achieve the same functionality with Java?
Thank you
Yes there is.
Here a simple example:
You can have a JSF(Java Server Faces) page, with a supporting backing bean that contains a method annotated with #PostConstruct This means that any action(for example downloading), will occur when the page is created.
There is already a question very similar already, have a look at: Invoke JSF managed bean action on page load
You can use Java's, URL class to download a file, but it requires a little work. You will need to do the following:
Create the URL object point at the file
Call openStream() to get an InputStream
Open the file you want to write to (a FileOutputStream)
Read from the InputStream and write to the file, until there is no more data left to read
Close the input and output streams
It doesn't really matter what type of file you are downloading (the fact that it's an executable file is irrelevant) since the process is the same for any type of file.
Update: It sounds like what you actually want is to plug the URL of a webpage into the Java app, and have the Java app find the link in the page and then download that link. If that is the case, the wording of your question is very unclear, but here are the basic steps I would use:
First, use steps 1 and 2 above to get an InputStream for the page
Use something like TagSoup or jsoup to parse the HTML
Find the <a> element that you want and extract its href attribute to get the URL of the file you need to download (if it's a relative URL instead of absolute, you will need to resolve that URL against the URL of the original page)
Use the steps above to download that URL
Here's a slight shortcut, based on jsoup (which I've never used before, I'm just writing this from snippets stolen from their webpage). I've left out a lot of error checking, but hey, I usually charge for this:
Document doc = Jsoup.connect(pageUrl).get();
Element aElement = doc.getElementsByTag("a").first() // Obviously you may need to refine this
String newUrl = aElement.attr("abs:href"); // This is a piece of jsoup magic that ensures that the destination URL is absolute
// assert newUrl != null
URL fileUrl = new URL(newUrl);
String destPath = fileUrl.getPath();
int lastSlash = destPath.lastIndexOf('/');
if (lastSlash != -1) {
destPath = destPath.substring(lastSlash);
}
// Assert that this is really a valid filename
// Now just download fileUrl and save it to destPath
The proper way to determine what the destination filename should be (unless you hardcode it) is actually to look for the Content-Disposition header, and look for the bit after filename=. In that case, you can't use openStream() on the URL, you will need to use openConnection() instead, to get a URLConnection. Then you can use getInputStream() to get your InputStream and getRequestProperty("Content-Disposition") to get the header to figure out your filename. In case that header is missing or malformed, you should then fall-back to using the method above to determine the destination filename.
You can do this using apache commons IO FileUtils
http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL, java.io.File)
Edit:
I was able to successfully download a zip file from source forge site (it is not empty), It did some thing like this
import java.io.File;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class Test
{
public static void main(String args[])
{
try {
URL url = new URL("http://sourceforge.net/projects/gallery/files/gallery3/3.0.2/gallery-3.0.2.zip/download");
FileUtils.copyURLToFile(url, new File("test.zip"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I was able successfully download tomcat.exe too
URL url = new URL("http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.exe");
I have a jsp written in which i am downloading certain files... they are pdf, zip, ppt and wmv. All the file types works except wmv. I couldnt figure out problem. When i play wmv file i get following error.
Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.
In my jps i have written code as following
response.setContentType("video/x-ms-wmv");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment; filename=123.wmv;");
String fileName = "/logs/164266828.wmv";
FileInputStream input = new FileInputStream(fileName);
BufferedInputStream buf = new BufferedInputStream(input);
int readBytes = 0;
ServletOutputStream myOut = response.getOutputStream( );
while((readBytes = buf.read( )) != -1)
myOut.write(readBytes);
Any inputs or modifications would be of great help !!!
Do not use JSP to stream binary data. JSP may have corrupted it with template text (whitespace and so on outside those <% %> things). Move this code to the doGet() method of a servlet class and invoke the servlet instead of JSP.
Unrelated to the concrete problem, those files seems static files. You don't necessarily need a servlet for this if you have full control over your server. In case of for example Tomcat, you could add the folder with the static files as another <Context> to the server.xml file.
See also:
Reliable data serving
Simplest way to serve static data from outside the application server in a Java web application
I have images on my file server which I want to embed into my email.
I can send email with local images like this way
message.addInline("image1", new ClassPathResource("../../static/images/123.jpg"));
but if i want to send email with my file server images, won't work.
message.addInline("image1", new ClassPathResource("http://fileserver.com/images/123.jpg"));
Anybody knows there is a way to do this?
This question is a year old, but I want to contribute for help other people...
Spring have ways to do the job that you want. Take a look in this chapter of the Spring 3x reference. Or Spring2x.
In resume:
You can obtain your image file from file file system of the server. As you can see in the reference:
Resource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);
Or from a relative path of the classpath of your application:
Resource res = new ClassPathResource("mx/blogspot/jesfre/test/image.png");
But, if you want to send a resource from a file server with a URL, you can do something like #Ralph said:
Url url = new URL("http://fileserver.com/images/123.jpg");
Resource res = new InputStreamResource(u.openStream());
And then, simply add the resource to your message:
helper.addInline("myIdentifier", res);
Hope this help somebody...
The problem is that http://fileserver.com/images/123.jpg is no Class Path Resource.
If you access the image from the file system then file access classes from java.io package.
If you really need to to access the files over http, then you need to download the file first.
Url url = new URL("http://fileserver.com/images/123.jpg");
InputStream is = u.openStream();
...