Hello.
I'm trying to do following:
1. I have a GWT-application with a form uploading text files in utf8
character encoding
2. Uploaded file content I save in the entity Blob field in datastore
3. I have a JavaFX client application getting this file content and
displaying it
Everything works just fine on dev environment but on production I have
problems with encoding - I get question marks on my javafx appet form.
More details.
1. On Form submit we go to
public class Upload extends HttpServlet
which creates a jdo transfer object (dictionaryTO) with byte[] field
where we need to store a content of the file:
dictionaryTO.setFile(inputStreamToBytes(item.openStream()));
where (notice, I'm setting "utf8" there):
private byte[] inputStreamToBytes(InputStream stream)
throws IOException, IllegalArgumentException,
IllegalStateException
{
BufferedReader inputStream = null;
inputStream = new BufferedReader(new InputStreamReader
(stream,
"utf8"));
String str;
ByteArrayOutputStream out = new ByteArrayOutputStream
();
StringBuffer buffer = new StringBuffer();
while ((str = inputStream.readLine()) != null) {
....
buffer.append(str);
buffer.append("\n");
}
out.write(buffer.toString().getBytes());
out.close();
return out.toByteArray();
}
2. After that I need to transfer this object to next servlet
(dictionaryService) which will save this object as an entity in
datastore. Doing it this way:
PrintWriter out = null;
response.setCharacterEncoding("utf8");
response.setContentType("text/plain");
out = response.getWriter();
...
request.getSession().setAttribute("dictionary",
dictionaryTO);
response.sendRedirect("dictionaryService");
3. dictionaryService is:
public class DictionaryServiceImpl extends RemoteServiceServlet
implements DictionaryService
which gets a transfer object from session, converts it in jdo entity
object - dictionary. A field with file content is being converted from
byte[] into Blob in this way - new Blob(dictionaryTO).getFile()). And
then the object is saved in a standard way: pm.makePersistent
(dictionary).
4. After that we need to process a request from client javafx
application. All I need is two methods:
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Common method that performs http request to the servlet in
order to
* retrieve required data
*
* @param urlString servlet name to send request
* @param xml xml-body of request
* @return xml-response from servlet
* @throws java.lang.Exception
*/
static private String performHttpPostRequest(String urlString,
String xml) throws Exception {
URL url;
HttpURLConnection connection;
String response = "false";
url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setAllowUserInteraction(true);
connection.setRequestProperty("Content-Type", "application/x-
www-form-urlencoded;charset=utf8");
//connection.setRequestProperty("Content-Type", "text/
xml;charset=utf8");
connection.setRequestProperty("Accept-Charset", "utf8");
connection.getOutputStream().write(xml.getBytes());
InputStream inpStr;
inpStr = connection.getInputStream();
//System.out.println("+++ " + connection.getRequestProperty
("Content-Type"));
response = convertStreamToString(inpStr);
inpStr.close();
return response;
}
/**
* Converts input stream received from servlet into string.
*
* @param is input stream
* @return input stream as a string
*/
static private String convertStreamToString(InputStream is) /
*throws UnsupportedEncodingException*/ {
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
5. On the server side this request receives
public class GetDictionary extends HttpServlet
protected void doPost(HttpServletRequest request,
HttpServletResponse
response) throws ServletException, IOException {
response.setCharacterEncoding("utf8");
response.setContentType("text/plain");
...
Here I'm parse an xml-string containing an ID of dictionary to
retreive from datastore (and whose file content I need to send back to
client). dictId is String.
...
request.getSession().setAttribute
("dictionaryToGetDictionary",
dictId);
response.sendRedirect(response.encodeURL
("dictionaryService"));
}
6. After that dictionaryService (public class DictionaryServiceImpl
extends RemoteServiceServlet implements DictionaryService) will get an
entity object from datastore:
public DictionaryTransferObject get(Long id) {
PersistenceManager pm = PersistenceManagerHelper
.getPersistenceManager();
Query query = pm.newQuery(Dictionary.class);
query.setFilter("id == idParam");
query.declareParameters("Long idParam");
....
}
will parse field with file content using ENCODING="utf8"
public static List<WordBean> getLines(DictionaryTransferObject
dictionary) {
List<WordBean> wordsList = new ArrayList<WordBean>();
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new
InputStreamReader(
new ByteArrayInputStream
(dictionary.getFile()), ENCODING));
String str;
while ((str = inputStream.readLine()) != null)
{
...
and will form an xml-string like xmlString="<lines><s>line</
s><s>line</
s>...</lines>" and will send it to client:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
{
response.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
...
response.setContentType("text/
xml;charset=utf8");
out.print(xmlString);
out.close();
return;
}
On development environment everything works correctly on all stages,
but on production there are some problems and I cannot understand what
are the differences and where is a bug:
1. when I write a file content to datastore?
2. when I fetch it and send to client?
3. or when I receive it on the cient-side?
Any ideas?
Thank you,
Alexey
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---