I have a requirement where I want to pass multiple thumbnails to UI (javascript) as the response of GET Request.
Each thumbnail is a separate file, so essentially I want to pass multiple files.
Is downloading multiple files even a sane idea? If yes, how can we do using CXF JAX-RS ?
I tried below code which is not working.
#GET
#Path("/streamThumbnails")
#Produces("multipart/mixed")
public MultipartBody getBooks2() {
List<Attachment> atts = new LinkedList<Attachment>();
File thumbnail1 = new File("//D:/pdf2.pdf");
File thumbnail2 = new File("//D:/pdf3.pdf");
atts.add(new Attachment("thumbnail1", "application/pdf",thumbnail1));
atts.add(new Attachment("thumbnail2", "application/pdf",thumbnail2));
return new MultipartBody(atts, true);
}
Since you are talking about thumbnails I guess you want to download images, and not PDFs as in your example. Why not to stripe them together into a single image and have the client use them as CSS sprites?
Related
I am developing a GWT application. This application is running in a server. Well, I implement a button which calls a method that generates a local file in server side. However I would like to download/generate this file in client side. How could I do this in GWT?
Thanks
In our project we created a file on server on demand. When the file has been successful created we send notification to browser and created a link.
See servlet code:
public class DownloadServlet extends HttpServlet {
private FileManager fileManager;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String encodedFileName = req.getRequestURI().substring(
req.getContextPath().length() + req.getServletPath().length() + 1);
String decodedFileName = URLDecoder.decode(encodedFileName, "utf-8");
File downloadableFile = fileManager.toFile(decodedFileName);
ServletOutputStream os = resp.getOutputStream();
try {
InputStream is = FileUtils.openInputStream(downloadableFile);
try {
IOUtils.copy(is, os);
} finally {
is.close();
}
} finally {
os.close();
}
}
}
private native void Download(String filename, String text)/*-{
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom); }-*/;
Use JSNI method inside GWT code , provide the file name you want to download in addition to JSON string as text (String) , this method will download a file with specified content in text variable to client browser.
Current situation is, that not all browsers are able to work with local file system, so there is no universal solution in GWT. Also as far as I know FilesSstem API is not finished.
As alternative you can keep using serverside generated files, or use Flash plugin to generate and store files (you will have to create a Flash app, and create some API to control it from GWT).
You should definitely have a look at Aki Miyazaki’s HTML5 file download code for GWT.
It works on the client side as you requested.
AFAIK, as of now, it only works in Chrome, but this is supposed to change as other browsers implement the download attribute.
You can do that using Data URIs:
Make your GWT RPC method return the file content or the data to generate the file.
On the client side, format a Data URI with the file content received or generate the data content.
Use Window.open to open a file save dialog passing the formatted DataURI.
Take a look at this reference, to understand the Data URI usage:
Export to csv in jQuery
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);
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();
...
I have a scheduled task running in JBoss 5.1 on a daily basis for sending birthday wishes.
The mail content is HTML and I embed images in the mail.
Now I would like to get the path of that image for embedding, how would it be possible to get path of image in a non-servelt environment.
Ofcourse I could have placed the images at a static location and accessed them, for which I don't want to hardcode the path.
The image is at "WebContent/images/birthday.jpg" location.
How are you generating the email content? Are these also static html files?
If you are going to use simple static html files, you will have to hard code the image paths. There is no other way around it.
You could write a simple Java application, which runs as a standalone application (without any servers,servlets etc), which will create the email content.
The java code can send out the emails for you too if you want.
These are some of the things you can do, if you use java
Use property files to specify the location of images. These are files which hold simple key/value pairs.
You can easily create multiple email content to different users, with the same template.
You will be able to easily redesign the html content for multiple users.
An example of using property files.
Create a file ex: "email_template.properties"
Enter the following into the file and save it.
image_server=http://www.mywebsite.com
image_folder=/WebContent/images/
Create a jave program to create your html email, and use the property file to generate the image locations.
Properties properties = new Properties();
try
{
properties.load(new FileInputStream("C://email_template.properties")); //specify path here
String sServerLocation = properties.getProperty("image_server");
String sImageFolder = properties.getProperty("image_folder");
StringBuilder strEmail = new StringBuilder();
strEmail.append("<html><body> <img src=\"" + sServerLocation + sImageFolder +"birthday.jsp\""> </body> </html>" );
// Write code to generate complete email dynamically
// write code to send out the email or to save as html file to you machine, where you can send it manually.
} catch (IOException e)
{
//
}
You get the idea. using plain html you will have to hard code.
However if you use a simple java file you can get more flexibility.
If you need code to send out email from java, check this link out.
How can I send an email by Java application using GMail, Yahoo, or Hotmail?
What is the best way to upload a directory in grails ?
I try this code :
def upload = {
if(request.method == 'POST') {
Iterator itr = request.getFileNames();
while(itr.hasNext()) {
MultipartFile file = request.getFile(itr.next());
File destination = new File(file.getOriginalFilename())
if (!file.isEmpty()) {
file.transferTo(destination)
// success
}
else
{
// failure
}
}
response.sendError(200,'Done');
}
}
Unfortunately, I can only upload file by file.
I would like to define my directory, and upload all files directly.
Any ideas ?
There is one major misconception here. The code which you posted will only work if both the server and the client runs at physically the same machine (which won't occur in real world) and if you're using the MSIE browser which has the misbehaviour to send the full path along the filename.
You should in fact get the contents of the uploaded file as an InputStream and write it to any OutputStream the usual Java IO way. The filename can be used to create a file with the same name at the server side, but you'll ensure that you strip the incorrectly by MSIE sent path from the filename.
As to your actual functional requirement, HTML doesn't provide facilities to upload complete directories or multiple files by a single <input type="file"> element. You'll need to create a client application which is capable of this and serve this from your webpage, like a Java Applet using Swing JFileChooser. There exist 3rd party solutions for this, like JumpLoader.