How to add both character encoding and the attachment for response? - java

I have a requirement to set both character encoding and the attachment for the responseheader
during file download. How can I do that?
ResponseBuilder lResBuild = Response.ok(lResult);
lResBuild.type("application/vnd.ms-excel");
lResBuild.header("Content-Type", "text/xml; charset=UTF-16LE");
lResponse = lResBuild.build();
lResponse = Response.ok(lResponse).header("Content-Disposition",
"attachment; filename = " + lFileName + ".xls").build();
Something like this. For special characters I am getting underscore.

Related

GB18030 characters gets replaced with empty space for file name while downloading a file

Hello Forum members,
I am not able to find out the reason for GB18030 characters gets replaced with empty spaces when I am downloading a file. Attached my code for your reference.
request.setCharacterEncoding("UTF-8");
String keyname = request.getParameter("keyname");
String format = request.getParameter("format");
// To be executed during check-out operation only.
if (format != null && keyname != null) {
ByteArrayOutputStream inStream = null;
try {
pgpPublicKeyInfoDb = new PGPPublicKeyInfoDb();
inStream = pgpPublicKeyInfoDb.checkOutPGPPublicKey(keyname, format);
response.setContentLength(inStream.size());
if (format.equals("ASC")) {
response.setContentType("application/asc; charset=UTF-8");
response.setHeader("Content-Disposition", "filename=" + keyname + ".asc");
} else {
response.setContentType("application/pgp; charset=UTF-8");
response.setHeader("Content-Disposition", "filename=" + keyname + ".pgp");
}
inStream.writeTo(response.getOutputStream());
non-ASCII characters in filename parameters are invalid, and browsers vary in what they do with them.
If you need a reliable and portable solution, you need to use UTF-8 and the escaping defined in https://greenbytes.de/tech/webdav/rfc6266.html.

In Java , Exclude a string while encode using base64

Is there a way that I can exclude particular string (II*) while encode a tiff file and keep that String as it is and also during the decode ?
Or
How can I specify the encoding to always encode (II*) as three characters and not to combine with any other characters ?
Below code to replace the string II* with ( II* ), however tiff got corrupted after that.
Path path = Paths.get("D:\\Users\\Vinoth\\workspace\\Testing\\Testing.tiff");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
content = content.replace("II*", " II ");
content = content.replace(" II "," II* ");
Files.write(path, content.getBytes(charset));

Java mailto Illegal character colon?

im trying to send an email with an attachment, but it keeps saying:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Illegal character in opaque part at index 64: mailto:recipient#mailserver.com?subject=ThePDFFile&attachment=C:\Users\Rascal\AppData\Local\Temp\FreelancerList-16-12-2014_09-227568200505392670736.doc
Java Code:
Desktop desktop = Desktop.getDesktop();
String message = "mailto:recipient#mailserver.com?subject=ThePDFFile&attachment=\""+path;
URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(message);
desktop.mail(uri);
Should be the colon right? But why???
You're calling URLEncoder.encode, but ignoring the result. I suspect you were trying to achieve something like this:
String encoded = URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(encoded);
... although at that point you'll have encoded the colon after the mailto part as well. I suspect you really want something like:
String query = "subject=ThePDFFile&attachment=\""+path;
String prefix = "mailto:recipient#mailserver.com?";
URI uri = URI.create(prefix + URLEncoder.encode(query, "UTF-8"));
Or even encoding just the values:
String query = "subject=" + URLEncoder.encode(subject, "UTF-8");
+ "&attachment=" + URLEncoder.encode(path, "UTF-8"));
URI uri = URI.create("mailto:recipient#mailserver.com?" + query);
... or create the URI from the various different parts separately, of course.

Server side URL Encoding

How can we Sanitize, via URL/HTML encoding, potentially danger characters & (< > “ ; /) in the server side, i am using encodeURI() and escape() in jsp page to encode on client side and i used URLEncoder.encode() in my java file to handel server side but it not encoding.
String needsEncodingPart = "?!##$%^&*() <>/\"'[]{}\"";
String baseURL = "http://url:80/test";
String encodedPart = URLEncoder.encode(needsEncodingPart,"UTF-8").replace("+", "%20");
System.out.println(baseURL + "/" + encodedPart);
needs to be replaced with "%20" as URLEncoder basically works with HTML type of encoding which replaces spaces with +
2nd way is to use java.net.URI
URL url = new URL("http://url:80/test/test1?!##$%^&*() <>/\"'[]{}\"");
URI uri = null;
uri = new URI(url.getProtocol(), url.getHost() + ":" + url.getPort(), url.getPath(), url.getQuery(), null);
uri.toString() will return encoded url. But in case of # encountered it might create some issue in encoding.
Thanks,
Gaurav

viewing XML data saved in BLOB Column via Java Servlet

I read the data from BLOB in Oracle, then assign to an object as byte []. I know the data is UTF-8 encoded XML therefore create a big String from the raw byte array and write directly to the PrintWriter of the ServletResponse object.
The response is expected to be downloaded on the client browser side as an attachment. But
the downloaded file is corrupt, the encoding is not correctly set in my opinion. Should I not set the HTTP Headers explicitly? I cannot figure out why I cannot reconstruct the XML as it is in the Database. any idea?
String filename = data_src.getFilename() + ".xml";
byte [] data = data_src.getByte_data();
String xml_UTF_8 = new String(data, "UTF-8");
// Init Servlet Response.
response.reset();
response.setHeader("Content-Type", "application/xml;charset=utf-8");
response.setHeader("Content-Encoding", "UTF-8");
response.setHeader("Content-Length", "" + xml_UTF_8.length() /*data.length ???*/);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); //inline vs attachment
PrintWriter writer = response.getWriter();
writer.write(xml_UTF_8);
writer.flush();
writer.close();
} finally {
// close streams.
response.flushBuffer();
}
// Inform JSF that it doesn't need to handle the response.
// This is very important, otherwise you will get the following exception in the logs:
// java.lang.IllegalStateException: Cannot forward after response has been committed.
facesContext.responseComplete();
}`

Categories

Resources