Naming my inline pdf - java

I am creating a pdf in a java servlet, and when my created pdf opens it incorrectly names that pdf after my servlet. Here is my code:
response.setHeader(contentDisposition, "inline; filename=TemporaryVerification.pdf");
try {
reader = new PdfReader(CreateStuVerification.class.getResource("/resource/" + tempFile));
stamp = new PdfStamper(reader, response.getOutputStream());
So I want my pdf to be named "TemporaryVerification.pdf", but it is name "CreateStuVerification.pdf"(which is the name of my servlet). Does anyone know why this is, and possibly how to correct it?
EDIT: I do have to keep it as an inline pdf. Though when I tried it as an attachment, it was correctly named.

This behavior is specific to the IE browser. It uses the last part of the URI path as default name of the downloaded resource. The more sane browsers properly use the filename attribute of the Content-Disposition header for this.
You'd better remap your servlet on a path pattern such as /pdf/* and then append the desired filename straight into the URL which is supposed to return the PDF like so
<a href="pdf/TemporaryVerification.pdf">
You can if necessary get the filename part in the servlet as follows:
String filename = request.getPathInfo().substring(1); // TemporaryVerification.pdf
// ...

Related

determine the icon for mime attachments in lotus domino

I am using the Java-API for Lotus Domino. Current version of Domino is 8.5. I connect to Domino over DIIOP/CORBA. I want to create a document (email) with attachments. The email is a multipart MIME document. Attaching a file ist quite easy and works perfectly. However the attachment gets a generic document icon (empty gray page). I would like to set an appropriate icon for the actual content type - e.g. a word-icon for a .doc-file. Is this possible in any way? I have tried to modify the content type parameter of the setContentFromBytes-method but to no avail.
here's a sample code:
session.setConvertMIME(false);
File file = new File("c:\\temp\\file.docx");
MIMEEntity child = document.createChildEntity();
MIMEHeader header = child.createHeader("Content-Disposition");
header.setHeaderVal("attachment; filename=" + file.getName());
header = child.createHeader("Content-ID");
header.setHeaderVal(file.getName());
Stream stream = session.createStream();
stream.open(file.getAbsolutePath(), "binary");
child.setContentFromBytes(stream,"application/vnd.openxmlformats-officedocument.wordprocessingml.document", MIMEEntity.ENC_IDENTITY_BINARY);
child.encodeContent(MIMEEntity.ENC_BASE64);
stream.close();
stream.truncate();
// other code here...
session.setConvertMIME(true);
I have also tried the older "application/msword" content type but it also dont work.
Any ideas how to set the icon for the attachment?
Thanks!
I don't think it is possible.
I believe the icon is looked up (from the registry) on the system that executes the code that attaches the file. Since you are using DIIOP/CORBA, that lookup occurs on the server. It's unlikely that the server has the Office software installed so it can't find the icon and just uses the generic one.

FreeMarker Not Able Display Chinese Character

First time use FreeMarker on JAVA project and stack on configure the chinese character.
I tried a lot of examples to fix the code like below, but it still not able to make it.
// Free-marker configuration object
Configuration conf = new Configuration();
conf.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));
conf.setLocale(Locale.CHINA);
conf.setDefaultEncoding("UTF-8");
// Load template from source folder
Template template = conf.getTemplate(templatePath);
template.setEncoding("UTF-8");
// Get Free-Marker output value
Writer output = new StringWriter();
template.process(input, output);
// Map Email Full Content
EmailNotification email = new EmailNotification();
email.setSubject(subject);
.......
Saw some example request to make changes on the freemarker.properties but i have no this file. I just import the .jar file and use it.
Kindly advise what should i do to make it display chinese character.
What exactly is the problem?
Anyway, cfg.setDefaultEncoding("UTF-8"); should be enough, assuming your template files are indeed in UTF-8. But, another place where you have to ensure proper encoding is when you convert the the template output back to "binary" from UNICODE text. So FreeMarker sends its output into a Writer, so everything is UNICODE so far, but then you will have an OutputStreamWriter or something like that, and that has to use charset (UTF-8 probably) that can encode Chinese characters.
You need to change your file encoding of your .ftl template files by saving over them in your IDE or notepad, and changing the encoding in the save dialog.
There should be an Encoding dropdown at the bottom of the save dialog.

java.nio.charset.IllegalCharsetNameException: 'ISO-8859-1'

Jsoup.connect("http://www.design.cmu.edu/community.php?s=3").get();
Could someone please show me why the code gave me the error:
java.nio.charset.IllegalCharsetNameException: 'ISO-8859-1'
The problem is in the target page. It is not well-formed at all.
When parsing the page, JSoup tries to fix the page and for one thing, parses the content type to "text/html; charset='iso-8859-1'"(with the single quotes included).
It then passes this string(with the single quotes) and uses it to get the charset:
Charset.forName("'ISO-8859-1'");
which fails.
The problem is in the target page.
Maybe you can use this alternative instead, which doesn't parse the charset from the page, because you explicitly pass it along:
String url = "http://www.design.cmu.edu/community.php?s=3";
Document document = Jsoup.parse(new URL(url).openStream(), "ISO-8859-1", url);

Returned URL as String is not valid in JSF

I'm trying to make use of google api as text-to-speech. So, I build a String then should pass it as a URL to a component to obtain a MP3 with the spoken words.
So, this is my code:
URI uri = new URI("http://translate.google.com/translate_tts?tl=es&q="+ URLEncoder.encode((String)this.text.getValue(), "UTF-8"));
When I make uri.toString() its return a well formed URL. If I copy and paste this output in the browser works pefectly.
But if I assign this returned String to the source property of a ice:outputMedia is not working. Then inspect the HTML generated in the page and the String in src property is:
http://translate.google.com/translate_tts?tl=es&q=Bobby+need+peanuts
The & symbol has been replaced by &.
How can I avoid this to make a valid URL?
You need to decode the url on the client side using Javascript.
var decoded = decodeURI(URI)

Chrome appends hyphen "-" to the downloaded CSV file

My code works for IE and FF. In Chrome, the browser appends "-" hyphen to the start and end of the file name, thus making it unable to recognize the file type. On renaming the file while saving as csv makes it open in Excel in a single cell but I want a solution to handle it in the code side. It seems difficult.
Below is my code:
//fileName = xxxx.csv
response.setContentType("application/download");
response.setHeader("Cache-Control", "public");
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename= \"" + fileName + "\"");
Note: I searched many blogs but didn't find the solution for Chrome.
I'm seeing two things:
You shouldn't have a space after filename= in your Content-Disposition.
I've never used quotes around the filename; I don't think you're meant to. I don't see anything about using quotes in the RFC, so I'd get rid of them.
I expect you're seeing a hyphen because Chrome is replacing either the space or (more likely) the quotes with hyphens because it considers the quotes invalid characters for the filename on your OS.
FWIW, my working Content-Disposition headers look like this:
Content-Disposition: attachment;filename=someFileName.csv
Off-topic: Re your statement:
My code works for IE and FF. In Chrome, the browser appends "-" hypen to the start and end of the file name, thus making it unable to recognize the file type.
Browsers should (and mostly do) recognise file type by the MIME type, not the file extension. You might want to set Content-Type to text/csv rather than application/octet-stream. (Of course, if you're talking about what the OS does with the file later, that's another thing.)
It's a bug/feature of Chrome. https://code.google.com/p/chromium/issues/detail?id=69939
Looks like Chrome considers the quotes part of the filename but
realizes that they are valid characters for a filename and so converts
them to hyphens.
RFC2183 defines the Content-Disposition header but does not say what
should happen if the server encloses the filename in quotes.
http://www.ietf.org/rfc/rfc2183.txt
(from 1st comment)
You must remove quotes from filename part of the Content-Disposition. I'm not sure what happens with blanks in filenames without quotes. That needs to be tested.
Although invalid characters can make this occur, this trailing hyphen filename issue only happens for me in different circumstances than those listed by the answer answers.
Long yesterday = new Date().getTime() - (1000*60*60*24);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String theCsv = "stuff,stuff\n yeah, uhuh\n";
//This sends a normal file
Attachment att = new Attachment("myfile.csv", theCsv.getBytes(), "UTF-8");
sendEmailWithAttachment("you#you.com","Me","me#me.com","Stuff for "+sdf.format(yesterday) ,"", att);
//This sends one with a trailing hyphen!!
String fileName = sdf.format(yesterday)+"-myfile.csv";
fileName = fileName.replaceAll(" ", "").replaceAll("\"", "");
Attachment att = new Attachment( fileName, theCsv.getBytes(), "UTF-8");
sendEmailWithAttachment("you#you.com", "Me","me#me.com","Stuff for "+sdf.format(yesterday),"", att);
I can't explain this. The problem in my case isn't quotes or spaces--the only difference is the pre-parsed filename. Maybe try a filename without string concats?
$response->sendHeaders();
$response->sendContent();
Replace above two lines with below single line of code.
$response->send();
It would work probably.

Categories