How to Posting Form Data from android? - java

I want to use Android java "emulate" a browser and post Login data. I tried this How to submit HTML form data (method=POST) using JAVA
But I failed.....
url:http://192.168.254.34/Control?id=1000
Here are the html source:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>欢迎使用WLAN无线宽带</title>
<style><!--
a {text-decoration: none}
.f1 { font-size: 10.5pt; font-family: Verdana; line-height: 12pt }
.f2 { font-size: 8pt; font-family: Verdana; line-height: 12pt }
.btn {
BORDER-LEFT: #7b9ebd 1px solid;
BORDER-RIGHT: #7b9ebd 1px solid;
BORDER-TOP: #7b9ebd 1px solid;
BORDER-BOTTOM: #7b9ebd 1px solid;
PADDING-LEFT: 2px;
PADDING-RIGHT: 2px;
PADDING-TOP: 2px;
FONT-SIZE: 10.5pt;
CURSOR: hand;
COLOR: black;
FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cecfde);
}
-->
</style>
<base href="http://192.168.254.34/pageset/131/PDA/">
</head><body topmargin="0" leftmargin="0">
<!-- 本地动态密码开始-->
<script>
function submitform2(){
if (form1.strAccount.value==""){
alert("请填入用户名!");
return;
}
form2.num.value=form1.strAccount.value;
form2.submit();
}
</script>
<iframe scr="about:blank" width="0" height="0" name="a"></iframe>
<FORM name="form2" method=post action="http://218.204.223.213:8080/cmpp/service.do?id=newuser" target="a">
<input size=12 type=hidden name="num" maxlength=26 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:" size="14"></td>
<INPUT value="172.31.18.199" type=hidden name=ip>
</FORM>
<!-- 本地动态密码结束 --><form name=form1 method=post action="/Control?id=2000">
<center>
<table width=600 border=0 height=800>
<tr>
<td height=800>
<div align=center>
<center>
<table width=600 border=0 height=800 bgcolor=#FFFFFF cellspacing=0 cellpadding=0 style="border-collapse: collapse" bordercolor=#111111>
<tr>
<td height=800 width=600 background="a02.jpg" align="center">
<table border="0" width="288" cellspacing="0" cellpadding="0" height="97%">
<tr>
<td height="82" colspan="2">
<p align="center" class="f1">
 </td>
</tr>
<tr>
<td height="295" align="center" colspan="2">
<table border="0" width="104%" cellspacing="0" cellpadding="0" height="106%">
<tr>
<td height="148" colspan="2">
<p align="center">
<font color="#333399" style="font-size: 10.5pt">
欢迎使用WLAN无线登陆页面</font></td>
</tr>
<tr>
<td width="32%" height="33" align="right" class="f1">
用户帐号</td>
<td height="33" width="68%">
<input name=strAccount maxlength=26 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:" size="21"></td>
</tr>
<tr>
<td width="32%" align="right" class="f1">用户密码</td>
<td width="68%">
<input type=password name=strPassword maxlength=16 style="border:1px solid #999999; FONT-SIZE: 10.5pt; BACKGROUND: #ffffff; COLOR: #333399; FONT-FAMILY: verdana; 1px:" size="21"></td>
</tr>
<tr>
<td height="96" colspan="2" align="left">
<p align="center">
<input type="button" value="登 录" width="86" height="23" name="I1" onclick="javascript:form1.submit();" class="btn">
<input type="reset" value="重 置" width="86" height="23" name="I2" class="btn"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="f1" width="255">
<p align="center"> </td>
<td class="f1" width="33">
 </td>
</tr>
<tr>
<td class="f1" height="91" width="255">
<p align="center" class="f1">
 </td>
<td class="f1" height="91" width="33">
 </td>
</tr>
<tr>
<td height="108" colspan="2">
<p align="center">
<br>
</td>
</tr>
</table>
</td>
</tr>
</table></center></div></td></tr></table></center></form></body></html>
the header I get:(use chrome)
Request URL:http://192.168.254.34/Control?id=2000
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=45129DDCC372B780F65344DB9DB107FB
Host:192.168.254.34
Origin:http://192.168.254.34
Referer:http://192.168.254.34/Control?id=1000
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.107 Safari/535.1
Query String Parametersview URL encoded
id:2000
Form Dataview URL encoded
strAccount:username
strPassword:password
Response Headersview source
Cache-Control:no-cache
Content-Length:2202
Content-Type:text/html;charset=GBK
Date:Mon, 08 Aug 2011 11:04:25 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:No-cache
Server:Apache-Coyote/1.1
Set-Cookie:intPageSetID=131; Expires=Mon, 08-Aug-2011 11:04:55 GMT
anybody know how to post login data?????????

Here's how I do it:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.server.com/dologin.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "KennyStarfighter");
nameValuePairs.add(new BasicNameValuePair("password", "SecretPassword");
nameValuePairs.add(new BasicNameValuePair("redirect", "?controller=login&view=loggedonpage"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity he = response.getEntity();
Then I can for instance handle the response as XML.
xmlReader.parse(new InputSource(he.getContent()));

Use HttpClient. I can do the post and handle authentication. Doc here

Related

How to get HTML(tables) from email body using GMail API in Java

I have to read some confirmation emails which are developed by tables in HTML.
I'm using GMail API and Java programming language for it.
I'm doing automatization of the registration process so I have to get the link to the confirmation email and click on that confirm button by Selenium test.
In response I'm getting emails with only text, for eg if I send to my mail some random text, I can get that text in response, however, for table kinds of emails I'm getting messages that the body is null.
This is the HTML code of that email.
Image of the email: Image of email
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<title>Test</title>
<meta content="light only" name="color-scheme" />
<meta name="”x-apple-disable-message-reformatting”" />
<style></style>
</head>
<body class="full-width" data-gr-ext-installed="" data-new-gr-c-s-check-loaded="14.997.0" style="max-width: 600px;width: 100%;background: #333333; margin: 0 auto;">
<div class="full-width" style="width: 100%;padding: 1em 0;"><!--[if mso | IE]>
</td></tr></table>
<![endif]--><!--[if mso | IE]>
<table class="full-width" border="0" cellpadding="0" cellspacing="0" width="600" align="center" style="width: 100%;">
<tr style=" width: 100%;">
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div class="full-width" style=" width: 100%;margin:0 auto;max-width:600px;background:#333333;">
<table align="center" border="0" cellpadding="0" cellspacing="0" class="full-width" style="font-size:0px;width:100%;background:#1b2940;">
<tbody class="full-width">
<tr class="full-width">
<td class="full-width" style="text-align:center;vertical-align:top;font-size:0px;padding:0px;"><!--[if mso | IE]>
<table class="full-width" border="0" cellpadding="0" cellspacing="0">
<tr class="full-width">
<td class="full-width" style="vertical-align:middle;width:600px;">
<![endif]-->
<div aria-labelledby="mj-column-per-100" class="full-width" style="vertical-align:middle;display:inline-block;font-size:13px;text-align:left;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" class="full-width" style="vertical-align:middle;" width="100%">
<tbody class="full-width">
<tr>
<td align="center" style="word-break:break-word;font-size:0px;padding:0px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;border-spacing:0px;">
<tbody><!--
<tr>--><!-- <td style="width:600px;">--><!-- <a href="https://www.24bettle.com/?utm_source=system-email&utm_medium=Image"--><!-- target="_blank" >--><!-- <img alt="24Bettle Logo"--><!-- src="http://24bettle.com/images/email_images/button-gradient-pic.png"--><!-- style="outline: none; text-decoration: none; clear: both; border: 0px; max-width: 640px; width: 600px; height: 122px; "--><!-- title=""/>--><!-- </a>--><!-- </td>--><!-- </tr>-->
<tr>
<td style="width:650px"><img class="first-banner" src="https://24bettle.com/images/banners/24header-image-2.png" style="outline: none; text-decoration: none; clear: both; border: 0px; max-width: 600px; width: 100%;" /></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" style="word-break:break-word;font-size:0px;padding:0px;"> </td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td></tr></table>
<![endif]--></td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td></tr></table>
<![endif]--><!--[if mso | IE]>
<table border="0" cellpadding="0" cellspacing="0" width="600" align="center" style="width:600px;">
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div class="full-width" style=" width: 100%;margin:0 auto;max-width:600px;background:#fff;">
<table border="0" cellpadding="0" cellspacing="0" class="full-width" style="padding: 2em 2em;vertical-align:middle;" width="100%">
<tbody class="full-width">
<tr>
<td align="left" style="word-break:break-word;font-size:0px;padding:0px;">
<div style="cursor:auto;color:#000000;font-family:Verdana, sans-serif;font-size:20px;line-height:20px;">
<p style="font-size: 20px; margin: 0px 0px 15px; color: rgb(76, 76, 76); text-align: center;">Hello {username},</p>
</div>
</td>
</tr>
<tr class="full-width">
<td align="left" style="word-break:break-word;font-size:0px;padding:0px;">
<div style="cursor:auto;color:#000000;font-family:Verdana, sans-serif;font-size:14px;line-height:20px;">
<table border="0" cellpadding="0" cellspacing="0" style="font-size: 13px; background-color: rgb(255, 255, 255); vertical-align: middle;" width="100%">
<tbody>
<tr>
<td align="left" style="word-break: break-word; font-size: 0px; padding: 0px;">
<div style="cursor: auto; font-family: Verdana, sans-serif; font-size: 14px; line-height: 20px;">
<table border="0" cellpadding="0" cellspacing="0" style="font-family: Verdana, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); vertical-align: middle;" width="100%">
<tbody>
<tr>
<td align="left" style="word-break: break-word; font-size: 0px; padding: 0px;">
<div style="cursor: auto; font-size: 14px; line-height: 20px;">
<p><span style="font-family: verdana, geneva, sans-serif;"><span style="color:#333333;">Hit the link to confirm your shiny, new account and enter <strong>24Bettle </strong>- a new dimension of awesome!</span></span></p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td align="center" style="word-break:break-word;font-size:0px;padding:10px 25px;">
<table align="center" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr><!-- style="border: 3px solid #4A8FFF;box-shadow: 0px 5px 10px 0px rgba(85,108,108,1);display:inline-block;text-decoration:none;color:white;font-family:Verdana, sans-serif;font-size:22px;text-shadow: 0px 2px 0px #000000;line-height: 20px;font-weight:900;margin:0px; padding:22px 23px; background: transparent linear-gradient(180deg, RGB(118, 191, 254) 44%, #1A3380 97%) 0% 0% no-repeat padding-box; no-repeat:padding-box;border-radius: 35px"-->
<td align="center" bgcolor="" style="border-radius:6px;color:white;cursor:auto;background: url(http://24bettle.com/images/email_images/button-gradient-pic.png);background-size: 100% 100%;" valign="middle">CONFIRM!</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" style="word-break:break-word;font-size:0px;padding:0px;">
<div style="cursor:auto;color:#000000;font-family:Verdana, sans-serif;font-size:14px;line-height:14px;">
<div style="text-align: -webkit-center; background-color: rgb(255, 255, 255); cursor: auto; font-family: Verdana, sans-serif; font-size: 14px; line-height: 14px;"> </div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="full-width" style="margin:0 auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" class="full-width" style="font-size:0px;width:100%;background:#333333;">
<tbody class="full-width">
<tr>
<td style="text-align:center;vertical-align:top;font-size:0px;padding:0px;">
<div aria-labelledby="mj-column-per-100" class="mj-column-per-100" style="background-color: #333333;vertical-align:middle;display:inline-block;font-size:13px;text-align:center;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" style="vertical-align:middle;" width="100%">
<tbody style="background: #141E30; ">
<tr>
<td align="center" style="word-break:break-word;font-size:0px;padding: 10px 0 0 0;background-color: #333333">
<div style="cursor:auto;color:#CCCCCC;font-family:Verdana, sans-serif;line-height:22px;">
<p style="padding: 0 1em;font-family: 'Segoe UI',sans-serif;font-size: 14px; margin: 0; color: #CCCCCC;text-align: -webkit-center;"> </p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td></tr></table>
<![endif]--></td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td></tr></table>
<![endif]--><!--[if mso | IE]>
<table class="full-width" border="0" cellpadding="0" cellspacing="0" width="600" align="center" style="width:600px;">
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div class="full-width" style="margin:0 auto;max-width:600px;background:#fff;">
<table align="center" border="0" cellpadding="0" cellspacing="0" class="full-width" style="font-size:0px;width:100%;background:#fff;">
<tbody>
<tr>
<td style="text-align:center;vertical-align:top;font-size:0px;padding:0px;"><!--[if mso | IE]>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="vertical-align:middle;width:300px;">
<![endif]--><!--[if mso | IE]>
</td>
<td style="vertical-align:middle;width:300px;">
<![endif]--><!--[if mso | IE]>
</td></tr></table>
<![endif]--></td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td></tr></table>
<![endif]--></div>
</body>
</html>
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import com.google.api.client.repackaged.org.apache.commons.codec.binary.StringUtils;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.Gmail.Users;
import com.google.api.services.gmail.Gmail.Users.Messages;
import com.google.api.services.gmail.Gmail.Users.Messages.Get;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;
import com.google.api.services.gmail.model.ListMessagesResponse;
import com.google.api.services.gmail.model.Message;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
public class GmailQuickstart {
private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
/**
* Creates an authorized Credential object.
* #param HTTP_TRANSPORT The network HTTP Transport.
* #return An authorized Credential object.
* #throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String... args) throws IOException,
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
// Print the labels in the user's account.
String user = "me";
ListLabelsResponse listResponse = service.users().labels().list(user).execute();
List<Label> labels = listResponse.getLabels();
if (labels.isEmpty()) {
System.out.println("No labels found.");
} else {
System.out.println("Labels:");
for (Label label: labels) {
System.out.printf("- %s\n", label.getName());
}
}
// Access Gmail inbox
Gmail.Users.Messages.List request = service.users().messages().list(user).setQ("from: " + "helpdesk#b-bets.com");
ListMessagesResponse messagesResponse = request.execute();
request.setPageToken(messagesResponse.getNextPageToken());
// Get ID of the email you are looking for
Gmail.Users.Messages.List request = service.users().messages().list(user).setQ("from: " + "support1#24bettle.com");
String messageId = messagesResponse.getMessages().get(0).getId();
Message message = service.users().messages().get(user, messageId).execute();
// Print email body
String emailBody = StringUtils
.newStringUtf8(Base64.decodeBase64(message.getPayload().getParts().get(0).getBody().getData()));
System.out.println("Email body : " + emailBody);
}
}
Issue:
The HTML table you are looking for is in a subpart of the message's first part.
Explanation:
The response you are getting from Try this API has this approximate shape (excluding irrelevant fields):
{
"payload": {
"body": { "size": 0 },
"parts": [
{
"partId": "0",
"body": { "size": 0 },
"parts": [
{
"partId": "0.0",
// Other fields
},
{
"partId": "0.1",
"mimeType": "text/html",
"body": {
"size": 14024,
"data": "BASE64_ENCODED_STRING" // HTML table
},
// Other fields
}
]
},
{
"partId": "1",
// Other fields
}
]
},
// Other fields
}
If you look at this, it's clear that the content you are looking for is in the first part, but this part itself contains several subparts. The second of these parts contains the desired content.
Solution:
You should be able to retrieve the data from this part via:
message.getPayload().getParts().get(0).getParts().get(1).getBody().getData();
Therefore, replace this:
String emailBody = StringUtils
.newStringUtf8(Base64.decodeBase64(message.getPayload().getParts().get(0).getBody().getData()));
With this:
String emailBody = StringUtils
.newStringUtf8(Base64.decodeBase64(message.getPayload().getParts().get(0).getParts().get(1).getBody().getData()));
Reference:
MessagePart

Images not visible in Gmail sent through HTML

I am trying to send mails to user who signup through our portal. Everything works fine except that image is not visible in the mail. here is the snippet of code
<html>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor='#E5E5E5' >
<STYLE>
.headerTop { background-color:#FFCC66; border-top:0px solid #000000; border-bottom:1px solid #FFFFFF; text-align:center; }
.adminText { font-size:10px; color:#996600; line-height:200%; font-family:verdana; text-decoration:none; }
.headerBar { background-color:#FFFFFF; border-top:0px solid #333333; border-bottom:10px solid #FFFFFF; }
.title { font-size:16px; font-weight:bold; color:#295EA0; font-family:arial; line-height:110%; }
.subTitle { font-size:11px; font-weight:normal; color:#666666; font-style:italic; font-family:arial; }
.defaultText { font-size:12px; color:#000000; line-height:150%; font-family:arial; }
.footerRow { background-color:#FFFFCC; border-top:10px solid #FFFFFF; }
.footerText { font-size:10px; color:#996600; line-height:100%; font-family:verdana; }
a { color:#00F; color:#00F; color:#00F; }
</STYLE>
<table width="100%" cellpadding="10" cellspacing="0" class="backgroundTable" bgcolor='#E5E5E5' >
<tr>
<td valign="top" align="center">
<table width="550" cellpadding="0" cellspacing="0">
<tr>
<td style="background-color:#FFFFFF;border-top:0px solid #333333;border-bottom:10px solid #FFFFFF;"><center><IMG id=editableImg1 SRC="#banner#" BORDER="0" title="Reppify" alt="Reppify" align="center"></center></td>
</tr>
I think you need to write the image full path
<tr>
<td style="background-color:#FFFFFF;border-top:0px solid #333333;border-bottom:10px solid #FFFFFF;"><center><IMG id=editableImg1 SRC="**http://www.www.com/banner.jpg**" BORDER="0" title="Reppify" alt="Reppify" align="center"></center></td>
</tr>

BufferReader not able to read full file in java

Hello Friends I have following data in store in file.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<div id="divOldReceipt" style="width:100%">
<style>
.tableStyle{border-collapse: collapse;
width: 100%;
font-family: Times New Roman;
font-size: 17px;
border: 1px solid black;}
.tableStyle th td {border: 1px solid black;}
.tableStyle td {border: 1px solid black;padding-left:3px;}
</style>
<table cellspacing="0" cellpadding="0" style="width: 8in; font-family:Times New Roman;">
<tbody>
<tr>
<td style="width: 20%;text-align:center" rowspan="2">
<img style="height: 84px; width: 71px" src={%images_path%}></img>
</td>
<td style="width: 60%;text-align: center; font-size: 25px;">
<b>
CORPORATION OF CHENNAI
</b>
</td>
<td></td>
</tr>
<tr>
<td style="text-align: center; font-size: 18px">
<b>
PROPERTY TAX RECEIPT
</b>
</td>
<td></td>
</tr>
<tr>
<td colspan="3">
<table class="tableStyle" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 20%"></td>
<td style="width: 20%"></td>
<td style="width: 10%"></td>
<td style="width: 10%"></td>
<td style="width: 40%"></td>
</tr>
<tr>
<td style="height: 25px;">
<b>
Receipt No:
</b>
</td>
<td colspan="2" style="height: 25px;">
<b>
{%receiptNo%}
</b>
</td>
<td colspan="2" style="text-align:center;height: 25px;">
<b>
Receipt Date: {%Receipt_date%}
</b>
</td>
</tr>
<tr>
<td style="height: 25px;">
<b>
Name:
</b>
</td>
<td style="height: 25px;" colspan="4">
{%persone_name%}
</td>
</tr>
<tr>
<td style="height: 25px;">
<b>
Address:
</b>
</td>
<td style="height: 25px;" colspan="4">
{%address%}
</td>
</tr>
<tr>
<td rowspan="2" style="height: 25px;">
<b>
Payment Details:
</b>
</td>
<td style="height: 25px;border-right-style:none;">
Description:
</td>
<td colspan="3" style="height: 25px;border-left-style:none;">
New Property Tax Number : {%new_property_tax_no%}
<br>
Old Property Tax Number : {%old_property_tax_no%}
</td>
</tr>
<tr>
<td style="height: 25px;border-right-style:none;">
Paid By:
</td>
<td colspan="3" style="height: 25px;border-left-style:none;">
{%paid_by%}
</td>
</tr>
<tr>
<td style="text-align:center;height: 25px;" colspan="3">
<b>
Description Head of A/C
</b>
</td>
<td style="text-align:center;height: 25px;" colspan="2">
<b>
Amount (in Rupees)
</b>
</td>
</tr>
<tr>
<td style="height: 25px;text-align: center;" colspan="3">
{%installment%}
</td>
<td style="text-align:center;padding-right:5px;height : 25px;" colspan="2">
{%adjistment%}
</td>
</tr>
<tr>
<td style="text-align:right;padding-right:5px;height: 25px;" colspan="3">
<b>
Total:
</b>
</td>
<td style="text-align:center;padding-right:5px;height : 25px;" colspan="2">
<b>
{%adjistment%}
</b>
</td>
</tr>
{%Bank Details%}
<tr>
<td colspan="5" style="height: 25px;">
This is computer generated receipt. Signature is not neccessary.Except Online payment the receipt is subject to realisation of cheque.
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
I Want to read this file in java using following code
public String readTemplateFile(String fileName) throws Exception
{
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String line =null;
StringBuilder sb = new StringBuilder();
while((line = bufferedReader.readLine())!=null)
{
sb.append(line);
}
return sb.toString();
}
OR
public String readFile(String filepath) throws IOException {
File f = new File(filepath);
if (f.exists()) {
FileInputStream in = new FileInputStream(f);
int size = in.available();
byte c[] = new byte[size];
for (int i = 0; i < size; i++) {
c[i] = (byte) in.read();
}
String filedata = new String(c, "utf-8");
return filedata.toString().trim();
} else {
return null;
}
}
But Full file is not getting read
following is output
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <div id="divOldReceipt" style="width:100%"> <style> .tableStyle{border-collapse: collapse; width: 100%; font-family: Times New Roman; font-size: 17px; border: 1px solid black;} .tableStyle th td {border: 1px solid black;} .tableStyle td {border: 1px solid black;padding-left:3px;} </style> <table cellspacing="0" cellpadding="0" style="width: 8in; font-family:Times New Roman;"> <tbody> <tr> <td style="width: 20%;text-align:center" rowspan="2"> <img style="height: 84px; width: 71px" src="../Images/Corporation.gif"></img> </td> <td style="width: 60%;text-align: center; font-size: 25px;"> <b> CORPORATION OF CHENNAI </b> </td> <td></td> </tr> <tr> <td style="text-align: center; font-size: 18px"> <b> PROPERTY TAX RECEIPT </b> </td> <td></td> </tr> <tr> <td colspan="3"> <table class="tableStyle" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="width: 20%"></td> <td style="width: 20%"></td> <td style="width: 10%"></td> <td style="width: 10%"></td> <td style="width: 40%"></td> </tr> <tr> <td style="height: 25px;"> <b> Receipt No: </b> </td> <td colspan="2" style="height: 25px;"> <b> {%receiptNo%} </b> </td> <td colspan="2" style="text-align:center;height: 25px;"> <b> Receipt Date: {%Receipt_date%} </b> </td> </tr> <tr> <td style="height: 25px;"> <b> Name: </b> </td> <td style="height: 25px;" colspan="4"> {%persone_name%} </td> </tr> <tr> <td style="height: 25px;">
I am confused why my code is behaving like this can some one plz help me to solve this issue.
Thanks In advance
Is the output you mention produced by the first or by the second code sample?
For your first code example, note that bufferedReader.readLine() returns the next line of the file but discard the end-of-line characters. These end-of-line characters are not added again by the sb.append(line) method call. This might explain why the end-of-lines are removed from your file.
For your second code examle, in.available() only returns an estimate of the number of bytes remaining in the stream, so don't use to determine the size of the input. It is better to read through the stream sequentially until the end-of-file has been reached. If the end-of-file is reached, in.read() will return -1.

GWT grid widget with row & column headers?

I am looking for any GWT grid widget that needs to have row and column headers on X and Y axis. Those needs to be seamlessly scrollable across X and Y axis.
Essentially I am looking for a GWT widget as demonstrated in the below URL
jsfiddle.net/jschlick/Gv26h/
We need to prifix with http:// in the above URL. The above is completely implemented in Java script.
Is there any GWT widget available with above functionality?
Any help would be really appreciated.
<html>
<header>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
body, input {
/*font: 15px/1em Arial, Helvetica, sans serif;*/
}
-webkit-scrollbar {
height: 7px;
width: 7px;
-webkit-appearance: none;
}
-webkit-scrollbar-thumb {
background-color: rgba(0,0,0,.5);
border-radius: 4px;
-webkit-box-shadow: 0 0 1px rgba(255,255,255,.5);
}
#divHeader {
border-bottom: 1px solid #d7d7d7;
overflow: hidden;
padding: 0 0 5px 0;
width: 284px;
}
#firstcol {
border-right: 1px solid #d7d7d7;
height: 200px;
overflow: hidden;
padding: 0 10px;
}
#table_div {
height: 210px;
overflow: scroll;
position: relative;
width: 300px;
}
#table_div td {
}
</style>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
fnAdjustTable();
});
fnAdjustTable = function(){
var colCount = $('#firstTr>td').length; //get total number of column
var m = 0;
var n = 0;
var brow = 'mozilla';
jQuery.each(jQuery.browser, function(i, val) {
if(val == true){
brow = i.toString();
}
});
$('.tableHeader').each(function(i){
if (m < colCount){
if (brow == 'mozilla'){
$('#firstTd').css("width",$('.tableFirstCol').innerWidth());//for adjusting first td
$(this).css('width',$('#table_div td:eq('+m+')').innerWidth());//for assigning width to table Header div
}
else if (brow == 'msie'){
$('#firstTd').css("width",$('.tableFirstCol').width());
$(this).css('width',$('#table_div td:eq('+m+')').width()-2);//In IE there is difference of 2 px
}
else if (brow == 'safari'){
$('#firstTd').css("width",$('.tableFirstCol').width());
$(this).css('width',$('#table_div td:eq('+m+')').width());
}
else {
$('#firstTd').css("width",$('.tableFirstCol').width());
$(this).css('width',$('#table_div td:eq('+m+')').innerWidth());
}
}
m++;
});
$('.tableFirstCol').each(function(i){
if(brow == 'mozilla'){
$(this).css('height',$('#table_div td:eq('+colCount*n+')').outerHeight());//for providing height using scrollable table column height
}
else if(brow == 'msie'){
$(this).css('height',$('#table_div td:eq('+colCount*n+')').innerHeight()-2);
}
else {
$(this).css('height',$('#table_div td:eq('+colCount*n+')').height());
}
n++;
});
}
//function to support scrolling of title and first column
fnScroll = function(){
$('#divHeader').scrollLeft($('#table_div').scrollLeft());
$('#firstcol').scrollTop($('#table_div').scrollTop());
}
</script>
</header>
<body>
<table cellspacing="0" cellpadding="0" border="1" >
<tr>
<td id="firstTd">
</td>
<td>
<div id="divHeader">
<table cellspacing="0" cellpadding="10" border="1">
<tr>
<td>
<div class="tableHeader">28</div>
</td>
<td>
<div class="tableHeader">30</div>
</td>
<td>
<div class="tableHeader">32</div>
</td>
<td>
<div class="tableHeader">34</div>
</td>
<td>
<div class="tableHeader">36</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td valign="top">
<div id="firstcol">
<table cellspacing="0" cellpadding="10" border="1">
<tr>
<td class="tableFirstCol">32</td>
</tr>
<tr>
<td class="tableFirstCol">32.5</td>
</tr>
<tr>
<td class="tableFirstCol">33</td>
</tr>
<tr>
<td class="tableFirstCol">33.5</td>
</tr>
<tr>
<td class="tableFirstCol">34</td>
</tr>
<tr>
<td class="tableFirstCol">34.5</td>
</tr>
<tr>
<td class="tableFirstCol">36</td>
</tr>
<tr>
<td class="tableFirstCol">36.5</td>
</tr>
<tr>
<td class="tableFirstCol">38</td>
</tr>
</table>
</div>
</td>
<td valign="top">
<div id="table_div" onscroll="fnScroll()" >
<table width="500px" cellspacing="0" cellpadding="10" border="1">
<tr id="firstTr">
<td>Row1Col1</td>
<td>Row1Col2</td>
<td>Row1Col3</td>
<td>Row1Col4</td>
<td>Row1Col5</td>
</tr>
<tr>
<td>Row2Col1</td>
<td>Row2Col2</td>
<td>Row2Col3</td>
<td>Row2Col4</td>
<td>Row2Col5</td>
</tr>
<tr>
<td>Row3Col1</td>
<td>Row3Col2</td>
<td>Row3Col3</td>
<td>Row3Col4</td>
<td>Row3Col5</td>
</tr>
<tr>
<td>Row4Col1</td>
<td>Row4Col2</td>
<td>Row4Col3</td>
<td>Row4Col4</td>
<td>Row4Col5</td>
</tr>
<tr>
<td>Row5Col1</td>
<td>Row5Col2</td>
<td>Row5Col3</td>
<td>Row5Col4</td>
<td>Row5Col5</td>
</tr>
<tr>
<td>Row6Col1</td>
<td>Row6Col2</td>
<td>Row6Col3</td>
<td>Row6Col4</td>
<td>Row6Col5</td>
</tr>
<tr>
<td>Row7Col1</td>
<td>Row7Col2</td>
<td>Row7Col3</td>
<td>Row7Col4</td>
<td>Row7Col5</td>
</tr>
<tr>
<td>Row8Col1</td>
<td>Row8Col2</td>
<td>Row8Col3</td>
<td>Row8Col4</td>
<td>Row8Col5</td>
</tr>
<tr>
<td>Row9Col1</td>
<td>Row9Col2</td>
<td>Row9Col3</td>
<td>Row9Col4</td>
<td>Row9Col5</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</body>
</html>

Getting this "org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBox" while trying to render a pdf

I am getting this error while I am trying to render a pdf. I am using Nicolas Leroux's PDF module for the play framework.
This is the entire stackstrace: Pastebin. Googling only reveals 4 results. They suggest removing any float properties, and position attributes, which I have removed, jsut to give it a try. I am still facing this problem. Has anyone been able to solve the issue?
EDIT: This is the html that renders the pdf:
#{extends 'main.html' /} #{set title:'Salary Details' /}
<html>
<head>
<title>Employee Name</title>
<style>
body {
font: 9pt Arial, Helvetica, sans-serif;
}
.container {
margin: 0 auto;
width: 1000px;
border: 1px solid black;
padding: 12px;
}
.logoClass {
width: 200px;
height: 85px;;
}
.companyAddressClass {
width: 60%;
margin: 0 auto;
font-size: 17px;
font-weight: bold;
text-align: center;
}
.empClass table {
width: 100%;
border: 1px solid #ccc;
border-collapse: collapse;
margin: 10px 0px;
}
.empClass table th,td {
border: 1px solid #ccc;
padding: 5px 5px;
}
.empClass table th {
background: #f2f2f2;
}
</style>
</head>
<body onload="window.print();">
%{for(int i=0;i<dataList.size ();i=i+1){}%
<div class="container" style="page-break-after:#{if i == dataList.size()-1} avoid;#{/if} #{else} always; #{/else}">
<div class="logoClass"></div>
<div class="companyAddressClass">
${dataList.get(i).distributorName},<br /> ${dataList.get(i).city},<br />
${dataList.get(i).state} <br /> <br /> <span>Payslip for the
month of ${dataList.get(i).salaryMonth}</span>
</div>
<br class="clear" />
<div class="empClass">
<table border="1">
<tr>
<td>Employee No: ${dataList.get(i).employeeNumber}</td>
<td>Location: ${dataList.get(i).location}</td>
</tr>
<tr>
<td>Name: ${dataList.get(i).employeeName}</td>
<td>Salary for the M/O: ${dataList.get(i).salaryMonth}</td>
</tr>
<tr>
<td>Bank Name: ${dataList.get(i).bankName}</td>
<td>Designation: ${dataList.get(i).designation}</td>
</tr>
<tr>
<td>Bank Acc No: ${dataList.get(i).accountNumber}</td>
<td>Join Date: ${dataList.get(i).joinDate}</td>
</tr>
<tr>
<td>PF No: ${dataList.get(i).pfNumber}</td>
<td>Gender: ${dataList.get(i).gender}</td>
</tr>
<tr>
<td>ESI No: ${dataList.get(i).esiNumber}</td>
<td>LOP: ${dataList.get(i).lop}</td>
</tr>
<tr>
<td>PAN No: ${dataList.get(i).panNumber}</td>
<td>Days Worked: ${dataList.get(i).daysWorked}</td>
</tr>
</table>
</div>
<br />
<div class="empClass">
<table border="1">
<tr>
<th colspan="3">Earnings</th>
<th colspan="2">Deductions</th>
</tr>
<tr>
<th align="left">Header</th>
<th align="right">Actual</th>
<th align="right">Paid</th>
<th align="left">Header</th>
<th align="right">Paid</th>
</tr>
<tr>
<td>Basic</td>
<td align="right">${dataList.get(i).basic}</td>
<td align="right">${dataList.get(i).basic}</td>
<td>PF</td>
<td align="right">${dataList.get(i).pf}</td>
</tr>
<tr>
<td>HRA</td>
<td align="right">${dataList.get(i).hra}</td>
<td align="right">${dataList.get(i).hra}</td>
<td>ESICEE</td>
<td align="right">${dataList.get(i).esicee}</td>
</tr>
<tr>
<td>Conveyance Allowance</td>
<td align="right">${dataList.get(i).conveynanceAllowance}</td>
<td align="right">${dataList.get(i).conveynanceAllowance}</td>
<td>ESICEE(PWS)</td>
<td align="right">${dataList.get(i).esiceePWS}</td>
</tr>
<tr>
<td>Medical Allowance</td>
<td align="right">${dataList.get(i).medicalAllowances}</td>
<td align="right">${dataList.get(i).medicalAllowances}</td>
<td>PT</td>
<td align="right">${dataList.get(i).pt}</td>
</tr>
<tr>
<td>Special Allowance</td>
<td align="right">${dataList.get(i).specialAllowance}</td>
<td align="right">${dataList.get(i).specialAllowance}</td>
<td>PT(PWS)</td>
<td align="right">${dataList.get(i).ptPWS}</td>
</tr>
<tr>
<td>Incentives</td>
<td align="right">${dataList.get(i).midMonthAmountPWS}</td>
<td align="right">${dataList.get(i).midMonthAmountPWS}</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Other Allowances</td>
<td align="right">${dataList.get(i).otherAllowance}</td>
<td align="right">${dataList.get(i).otherAllowance}</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3" align="right">Total
Earnings:${dataList.get(i).totalEarnings}</td>
<td colspan="2" align="right">Total Deduction
:${dataList.get(i).totalDeduction}</td>
</tr>
<tr>
<td colspan="3"> </td>
<td colspan="2" align="right"><strong>Salary Credited
: ${dataList.get(i).salaryCredited}</strong></td>
</tr>
</table>
<table>
<thead>
<tr>
<th>Position Title</th>
<th>City Name</th>
<th>State Name</th>
</tr>
</thead>
<thead>
<tr>
..
..
..
..
<th>${dataList.get(i).positionTitle}</th>
<th>${dataList.get(i).cityName}</th>
<th>${dataList.get(i).stateName}</th>
</tr>
</thead>
</table>
<br>
<table>
<thead>
<tr>
<th>Some Header</th>
<th>Some Header</th>
<th>Some Header</th>
<th>Some Header</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">Values</td>
..
..
..
..
..
</tr>
</tbody>
</table>
</div>
<div>
<span style="font-size: 15px; font-weight: bold;"> Salary
Credited Amount(In Words): ${dataList.get(i).salaryCreditedInWord} </span>
</div>
</div>
<br />
<br />
%{}}%
</body>
</html>
I am using Windows 7 and Tomcat6 as the testing environment, the same environment that people over here are facing trouble with.
Thanks.
This commit is a fix for your problem. You can download the whole class and put it in your source code, so you haven't to rebuild the library jar.
If you are facing this issue, to fix this just add a span tag inside the td and add the style to that span tag. Example:
<td>
<span style = style="position: absolute;">
</td>
Basically if you add the style to the td, you would get the error because of the bug in fly saucer pdf.

Categories