I'm trying to write a PDF to the client dynamically using servlet/JSP and jquery. Here is what I have so far:
SERVLET
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static MimetypesFileTypeMap mtp = new MimetypesFileTypeMap();
public IndexServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Sysout of doGet from IndexServlet");
////INIT OF VARs
int pdfNum,
pdf;
String[] currentBatch;
String
mimeType = null,
fileID = null,
rtn = null,
docSelect = null,
docNumber = null,
docType = null,
batchName = null,
event = request.getParameter("EVENT"),
pendDir = "\\\\****\\****\\****\\****\\****\\****\\****";
///////////////////
if(event.equals("LOAD")){
System.out.println("You're now in the LOAD portion of the doGet");
PdfInit p = new PdfInit();
Batch b = p.getBatch();
currentBatch = b.pdfFiles;
batchName = b.batchName;
pdfNum = b.pdfNum;
Gson gson = new Gson();
String json = gson.toJson(b);
System.out.println(json);
response.setContentType("application/json");
response.getWriter().write(json);
}else if(event.equals("GETPDF")){
System.out.println("You're now in the GETPDF portion of the doGet");
PdfInit p = new PdfInit();
Batch b = p.getBatch();
currentBatch = b.pdfFiles;
batchName = b.batchName;
pdfNum = b.pdfNum;
pdf = Integer.parseInt(request.getParameter("id"));
String fileName = pendDir + "\\" + batchName + "\\" + currentBatch[pdf];
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayDataSource bais = new ByteArrayDataSource(bis, mimeType);
byte [] pdfArray = bais.toByteArray();
String encoded = DatatypeConverter.printBase64Binary(pdfArray);
Gson gson = new Gson();
String json = gson.toJson(encoded);
response.setContentType("application/json");
response.getWriter().write(json);
response.getOutputStream().flush();
response.getOutputStream().close();
JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="/Indexer/_js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="/Indexer/_js/jquery.form.js"></script>
</head>
<body>
<input type="button" value="Load Batch" id="LoadBatchBtn">
<p id="batchName"><b>Batch Name:</b> </p>
<p>Click on a document to view it.</p>
<ol id="pdfList"></ol>
<div id="pdfDiv"></div>
</body>
</html>
<script>
$(document).ready(function(){
$("#LoadBatchBtn").click(function(){
$.getJSON("IndexServlet", {"EVENT": "LOAD"}, function(data) {
$("#batchName").append(data.batchName);
for(var i = 0; i < data.pdfNum; i++ )
{
$("#pdfList").append("<li id=" + i + ">" + data.pdfFiles[i] + "</li>");
}
$("li").click(function(){
$("#pdfDiv").hide();
$.getJSON("IndexServlet", {id: this.id, "EVENT":"GETPDF"}, function(data){
var decodedData = window.atob(data);
$("#pdfDiv").html(decodedData).show();
});
});
});
});
});
</script>
Right now I am not getting anything at all on the JSP. However in the console I can see the base64 string being passed without issue. I know I most likely need to take this one step further, but I am not sure where to go from here.
EDIT
Okay now I am getting string output in the div. How can I turn this into data that the browser reads as a PDF?
Related
I want to create a list of uploaded files that are stored in a directory on my hard drive.
My Controller:
#Controller
class MyFileUploadController {
#RequestMapping(value = "/uploadOneFile", method = RequestMethod.GET)
public String uploadOneFileHandler(Model model) {
MyUploadForm myUploadForm = new MyUploadForm();
model.addAttribute("myUploadForm", myUploadForm);
return "uploadOneFile";
}
#RequestMapping(value = "/uploadOneFile", method = RequestMethod.POST)
public String uploadOneFileHandlerPOST(HttpServletRequest request, //
Model model, //
#ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
return this.doUpload(request, model, myUploadForm);
}
#RequestMapping(value = "/uploadMultiFile", method = RequestMethod.GET)
public String uploadMultiFileHandler(Model model) {
MyUploadForm myUploadForm = new MyUploadForm();
model.addAttribute("myUploadForm", myUploadForm);
return "uploadMultiFile";
}
#RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
public String uploadMultiFileHandlerPOST(HttpServletRequest request, //
Model model, //
#ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
return this.doUpload(request, model, myUploadForm);
}
private String doUpload(HttpServletRequest request, Model model, //
MyUploadForm myUploadForm) {
String description = myUploadForm.getDescription();
System.out.println("Description: " + description);
String uploadRootPath = request.getServletContext().getRealPath("upload");
System.out.println("uploadRootPath=" + uploadRootPath);
File uploadRootDir = new File("(directory)");
if (!uploadRootDir.exists()) {
uploadRootDir.mkdirs();
}
MultipartFile[] fileDatas = myUploadForm.getFileDatas();
List<File> uploadedFiles = new ArrayList<File>();
List<String> failedFiles = new ArrayList<String>();
for (MultipartFile fileData : fileDatas) {
String name = fileData.getOriginalFilename();
System.out.println("Client File Name = " + name);
if (name != null && name.length() > 0) {
try {
File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(new
FileOutputStream(serverFile));
stream.write(fileData.getBytes());
stream.close();
uploadedFiles.add(serverFile);
System.out.println("Write file: " + serverFile);
} catch (Exception e) {
System.out.println("Error Write file: " + name);
failedFiles.add(name);
}
}
}
model.addAttribute("description", description);
model.addAttribute("uploadedFiles", uploadedFiles);
model.addAttribute("failedFiles", failedFiles);
return "uploadResult";
}
}
MyUploadForm
public class MyUploadForm {
private String description;
private MultipartFile[] fileDatas;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public MultipartFile[] getFileDatas() {
return fileDatas;
}
public void setFileDatas(MultipartFile[] fileDatas) {
this.fileDatas = fileDatas;
}
}
The User can upload his files on the uploadOneFile.html.
uploadOneFile.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:c="http://java.sun.com/xml/ns/javaee">
<head>
<meta charset="UTF-8">
<title>Upload One File</title>
</head>
<body>
<th:block th:include="/_menu"></th:block>
<h3>Upload single file:</h3>
<form th:object="${myUploadForm}" method="POST"
action="" enctype="multipart/form-data">
Beschreibung:
<br>
<input th:field="*{description}" style="width:300px;"/>
<br/><br/>
File to upload: <input th:field="*{fileDatas}" type="file"/>
<br/>
<input type="submit" value="Upload">
</form>
</body>
</html>
The uploaded files should then be displayed on the index page. Aswell it should be possible to download the files with just clicking on them.
I'm a beginner in Spring Boot so can you help me? If you need more informations let it me know.
You can create a table on that page (html layout you can choose as per design etc..)
Main logic can be:-
Get list of file's from the directory.
Have the names of files stored in a SET or LIST or something of your choice.
Pass the previous list onto UI using some model via the index page controller.
Render the list of files.
Upon clicking the particular file, call the endpoint to download file by name.
Some Code of initial interest could be like below:-
File directoryPath = new File("D:\\PATH\\OF\\DIRECTORY");
FileFilter textFilefilter = new FileFilter(){
public boolean accept(File file) {
boolean isFile = file.isFile();
if (isFile) {
return true;
} else {
return false;
}
}
};
//List of all the files (only files)
File filesList[] = directoryPath.listFiles(textFilefilter);
System.out.println("List of the files in the specified directory:");
for(File file : filesList) {
System.out.println("File-name: "+file.getName());
System.out.println("File-path: "+file.getAbsolutePath());
System.out.println("Size: "+file.getTotalSpace());
System.out.println(" ");
}
Im trying to call a servlet from a form tag in JSP but the values on my input areas are returning null so I get a server error with java.lang.NullPointerException, which is weird since im filling up the data before hitting the submit button
here is the form:
<form action="FileUploadServlet" enctype="multipart/form-data" method="post">
<div class = "col-md-6 col-md-offset-4" id = "articleSection">
<div class = "row" id = "title">
<input type ="text" name = "title" rows = "2" cols = "50" placeholder = "Name of your Article..." id = "artText">
<input type="hidden" name="id" value = '<%=(Integer)session.getAttribute("id")%>'>
</div>
<div id = "image">
<input type="file" name="image" id="fileToUpload">
</div>
<div class = "col-md-2" id = "submit">
<button type="submit" id = "submitBtn" name = "submit" value="articleSubmit">Submit</button>
</div>
</div>
</form>
and here is the servlet:
public class FileUploadServlet extends HttpServlet {
public static final String UPLOAD_DIR = "uploads";
public String dbFileName = "";
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int id = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
Part part = request.getPart("image");//
String fileName = extractFileName(part);//file name
String applicationPath = getServletContext().getRealPath("");
String uploadPath = applicationPath + File.separator + UPLOAD_DIR;
System.out.println("applicationPath:" + applicationPath);
File fileUploadDirectory = new File(uploadPath);
if (!fileUploadDirectory.exists()) {
fileUploadDirectory.mkdirs();
}
String savePath = uploadPath + File.separator + fileName;
System.out.println("savePath: " + savePath);
String sRootPath = new File(savePath).getAbsolutePath();
System.out.println("sRootPath: " + sRootPath);
part.write(savePath + File.separator);
File fileSaveDir1 = new File(savePath);
dbFileName = UPLOAD_DIR + File.separator + fileName;
part.write(savePath + File.separator);
try {
Connection con = DatabaseConnection.getCon();
PreparedStatement pst = con.prepareStatement("insert into articles(title, date, user_id, image) values(?,?,?,?)");
pst.setString(1, title);
pst.setDate(2, sqlDate);
pst.setInt(3, id);
pst.setString(4, dbFileName);
pst.executeUpdate();
response.sendRedirect("articleDetails.jsp?name"+title);
} catch (Exception e) {
out.println(e);
}
}
private String extractFileName(Part part) {//This method will print the file name.
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return "";
}
}
If I change the form method to "GET" then it doesnt return null but I want to post so that I can insert files to the database
EDIT:
I was able to fix the nullPointerException just by moving the method before the enctypeso it would look like:
<form action="FileUploadServlet" method="POST" enctype="multipart/form-data">
</form>
But now I'm getting this error:
java.io.FileNotFoundException: C:\Users\ttcat\Documents\glassfish5\glassfish\domains\domain1\generated\jsp\JspIpProject\C:\Users\ttcat\Documents\NetBeansProjects\JspIpProject\build\web\uploads\amihan.jpg (The filename, directory name, or volume label syntax is incorrect)
how do I remove this path?C:\Users\ttcat\Documents\glassfish5\glassfish\domains\domain1\generated\jsp\JspIpProject\
Try to below annotations in your servelt
#WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})
#MultipartConfig(maxFileSize = 100 * 1024 * 1024) // 100MB max
public class FileUploadServlet extends FileUploadServlet {
I want my application to be able to upload images into a directory. The problem is that it sometimes only works after a few tries, although sometimes it works properly. Here is my code:
This is the DemoBean.java file
private Part file1;
private String imageName;
private static String getFilename(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public Part getFile1() {
return file1;
}
public void setFile1(Part file1) {
this.file1 = file1;
}
public String upload() throws IOException {
file1.write("" + getFilename(file1));
imageName = getFilename(file1);
String fileName = "";
try {
Part filePart = file1;
fileName = getFilename(filePart);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
File outputFilePath = new File("c:\\project-images\\" + fileName);
inputStream = filePart.getInputStream();
outputStream = new FileOutputStream(outputFilePath);
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (Exception e) {
System.out.println("shit happened" + e.getCause());
} finally {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
//reload();
return "";
}
public void reload() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}
}
And this is the xhtml file that uses the bean
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/templates/page-template.xhtml">
<ui:param name="pageTitle" value="Books"/>
<ui:define name="panel-main">
<section>
<h:form enctype="multipart/form-data">
<h2>Add a new Book</h2>
<h:panelGrid columns="2" styleClass="form-grid" columnClasses="form-column-label,form-column-input">
<h:inputFile value="#{demoBean.file1}">
<f:ajax event="change" listener="#{demoBean.upload()}" execute="#form" render="#form"/>
</h:inputFile>
<c:set value="#{demoBean.imageName}" target="#{newBooks.book}" property="image"/>
<h:outputLabel for="Price">Price:</h:outputLabel>
<h:inputText id="Price" value="#{newBooks.book.price}" size="20"/>
<h:outputText value=""/>
<h:commandButton value="Submit" action="#{newBooks.submit}">
</h:commandButton>
</h:panelGrid>
</h:form>
</section>
</ui:define>
</ui:composition>
</html>
I am doing a sample application for UTF-8 with Spring to support multi-language .
This is my JSP with Script ,
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Test</h1>
<input type="text" id="hide" />
<input type="text" id="message"/>
<button id="button">test</button>
<div id="messageDisplayArea"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
var contexPath = "<%=request.getContextPath() %>";
</script>
<script>
$('#button').on('click', sendMessage);
function sendMessage() {
var intxnId = $("#hide").val();
var message = $("#message").val();
alert("send : \n intxnId : " + intxnId + "\nmessage : " + message);
$.ajax({
type: "POST",
cache: false,
url: contexPath + "/test.html",
async: true,
data: "intxnId=" + intxnId + "&message=" + encodeURIComponent(message),
//dataType: "json",
dataType: "html",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
scriptCharset: "utf-8",
success: function(response) {
alert(response);
alert(response.message);
if (response !== null && response !== "" && response !== "null") {
var txt = '{"data":[' + response + ']}';
var json = eval("(" + txt + ")");
for (i = 0; i < json.data.length; i++) {
var data = json.data[i];
var name = data.name;
var message = data.message;
var time = data.time;
alert("Name : " + name + "\nMessage : " + message + "\ntime : " + time);
var createHTML = send(name, message, time);
$("#messageDisplayArea").append(createHTML);
}
;
}
},
error: function(e) {
alert('Error: ' + e);
},
});
function send(name , message , time){
var user = "<div id='user' class='fontStyle'>"+name+"</div>";
var msg = "<div id='msg' class='fontStyle'>"+message+"</div>";
var timeStamp = "<div id='time' class='fontStyle'>"+time+"</div>";
var row = "<div id='msgSet' >"+ user + msg + timeStamp +"</div>";
return row;
}
}
</script>
</body>
</html>
My Spring controller will be ,
#RequestMapping(value = "test.html", method=RequestMethod.POST , headers = "Accept=*",produces = "application/json; charset=utf-8")
public #ResponseBody String sendMessage(HttpSession session, #RequestParam String intxnId, #RequestParam String message, HttpServletRequest request, HttpServletResponse response) {
String contentType = "application/json; charset=utf-8";
response.setContentType(contentType);
try {
// request.setCharacterEncoding("utf-8");
request.setCharacterEncoding("application/json; charset=utf-8");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Send Message UTF-8 ----------------- " + message);
String json = null;
HashMap<String, String> result = new HashMap<String, String>();
result.put("name", "test");
result.put("message", message);
result.put("time", "time");
ObjectMapper map = new ObjectMapper();
if (!result.isEmpty()) {
try {
json = map.writeValueAsString(result);
System.out.println("Send Message :::::::: : " + json);
} catch (Exception e) {
e.printStackTrace();
}
}
return json;
}
My Controllers prints ,
Send Message UTF-8 ----------------- தமிழ் அரிச்சுவடி
Send Message :::::::: : {"message":"தமிழ் அரிச்சுவடி","time":"time","name":"test"}
In this my controller prints the local language . But in the JQuery success alert , I got message as ???? . I need to append the local language text in my JSP .
Hope our stack members will help me.
Following piece of code should solve your issue.
#RequestMapping(value="<your path>",produces = "application/json; charset=utf-8")
public #ResponseBody String sendMessage(#RequestParam String intxnId, #RequestParam String message) {
String json = null;
HashMap<String, String> result = new HashMap<String, String>();
result.put("name", "test");
result.put("message", message);
result.put("time", "time");
ObjectMapper map = new ObjectMapper();
if (!result.isEmpty()) {
try {
json = map.writeValueAsString(result);
System.out.println("Send Message :::::::: : " + json);
} catch (Exception e) {
e.printStackTrace();
}
}
return json;
}
This will produce UTF-8 ajax response.
Add mimeType:"application/json; charset=UTF-8" in jQuery.
I got the solution by changing the Controller as ,
#RequestMapping(value = "test.html", method=RequestMethod.POST , headers = "Accept=*",produces = "application/json; charset=utf-8")
public ResponseEntity<String> sendMessage(HttpSession session, #RequestParam String intxnId, #RequestParam String message, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Send Message UTF-8 ----------------- " + message);
String json = null;
HashMap<String, String> result = new HashMap<String, String>();
result.put("name", "test");
result.put("message", message);
result.put("time", "time");
ObjectMapper map = new ObjectMapper();
if (!result.isEmpty()) {
try {
json = map.writeValueAsString(result);
System.out.println("Send Message :::::::: : " + json);
} catch (Exception e) {
e.printStackTrace();
}
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "application/json; charset=utf-8");
return new ResponseEntity<String>(json, responseHeaders, HttpStatus.CREATED);
}
i use URLConnection to read a weburl, however sometimes it can not read full response (and sometimes it worked fine for the same url),
one url is:http://messages.yahoo.com/yahoo/Business_%26_Finance/Investments/Stocks_%28A_to_Z%29/Stocks_Y/index.html
below is one sample cut-off response
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Yahoo! Message Boards - Stocks Y</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="http://l.yimg.com/d/lib/mb/default_yui_ycs_20080219.css"/>
<!--CSS source files for the Calendar Control -->
<style>
#cal1 { width: 100%;}
#cal2 { width: 100%;}
</style>
</head>
<body>
<center>
<div id="doc-layout">
<link type="text/css" rel="stylesheet" href="http://l.yimg.com/a/lib/uh/15/css/uh_rsa-1.0.5.css" /><style type="text/css">#ygma div{clear:none;}#ygma #yahoo{padding-top:0;*padding-top:5px;}</style><script type="text/javascript">(function(){var h={};var setUp=function(){h = YAHOO.one.uh.hotlistInfo = {rd:"http://global.ard.yahoo.com",space:"/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=0",adid:"5857129",prop:"ymb",protocol:"http",host:"messages.yahoo.com",url:"http%3a%2f%2fmessages.yahoo.com%2fyahoo%2fBusiness_%2526_Finance%2fInvestments%2fStocks_%2528A_to_Z%2529%2fStocks_Y%2findex.html",spaceid:"389132707"};YAHOO.one.uh.translate=function(str){var set={yahoo_homepage:"http://www.yahoo.com/", hp_detect_script:"http://www.yahoo.com/includes/hdhpdetect.php", set_hp_script:"http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=1/SIG=10uacnjgh/*http://www.yahoo.com/bin/set", set_hp_firefox_instructions:["Drag the \"Y!\" and drop it onto the \"Home\" icon.", "Select \"Yes\" from the pop up window.", "Nothing, you're done."], close_this_window:"Close this window", set_hp_alternative_instructions1:"If this didn't work for you see ", detailed_set_hp_instructions:"detailed instructions", set_hp_alternative_instructions2:""};return set[str];};YAHOO.one.uh.Search=['ygmasearchInput', 'sat'];};if("undefined" !==typeof YAHOO && "undefined" !==typeof YAHOO.one && "undefined" !==typeof YAHOO.one.uh){setUp();}else{setTimeout(arguments.callee, 500);}})();</script><div id="ygma"><div id="ygmaheader"><div class="bd sp"><div id="ymenu" class="ygmaclr"><div id="mepanel"><ul id="mepanel-nav"><li class="me1"><em>New User? <a class="ygmasignup" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=2/SIG=1395kefdv/*https://edit.yahoo.com/config/eval_register?.done=http://messages.finance.yahoo.com&.src=quote&.intl=us">Register</a></em></li><li class="me2"><em>Sign In</em></li><li class="me3">Help</li></ul></div><div id="ygmapromo"><div id="ygmapromo-i"></div>
<style>
#ygma-s{display:none;}
#ygma #ygmapromo-i, #ygmabt #ygmapromo-i {display:inline;}
</style>
<script>
(function(){
window.YAHOO = window.YAHOO || {};
YAHOO.one = window.YAHOO.one || {};
YAHOO.one.uh = window.YAHOO.one.uh || {};
YAHOO.one.uh.popularSearches = function(data) {
var chop = function(s, n) {
if (s.length > n) {
return s.substring(0,n)+"...";
}
else return s;
}
var e = document.getElementById("ygmapromo") || document.getElementById("ygmabtpromo");
var e2 = document.getElementById("ygmapromo-i");
if (!data.query.results || data.query.results == "0" || !data.query.results.links) {
return;
}
var i = data.query.results.links;
var hd = i.text;
var fr = "&fr=ush-tts&fr2=ps";
e2.innerHTML = 'Trending: '+chop(hd, 20)+'';
};
var ie;
if (navigator.userAgent.match(/MSIE\s6/)) {
var D = new Date();
var yr = D.getFullYear();
var mt = D.getMonth()+1;
var dy = D.getDate();
var hr = D.getHours();
ie = '&cache=' + yr + mt + dy + hr;
}
else {
ie = "";
}
var y = document.getElementById("ygma") || document.getElementById("ygmabt");
var feed;
if (y.offsetWidth < 850){
feed = "http://query.yahooapis.com/v1/public/yql/uhTrending/cokeTrending3?format=json&callback=YAHOO.one.uh.popularSearches&_maxage=1800&diagnostics=false&limit=1";
}
else {
feed = "http://query.yahooapis.com/v1/public/yql/uhTrending/cokeTrending2?format=json&callback=YAHOO.one.uh.popularSearches&_maxage=1800&diagnostics=false&limit=1";
}
var h = document.getElementsByTagName("head").item(0);
var s = document.createElement("script");
s.setAttribute("type", "text/javascript");
s.setAttribute("charset", "utf-8");
s.setAttribute("src", feed + ie);
h.appendChild(s);
})();
</script>
<style>
#ygma ol.searches h4, #ygmabt ol.searches h4 {
font-size:100%;
font-weight:bold;
color:#333333;
text-align:left;
padding-bottom:3px;
margin:0;
}
#ygma ol.searches, #ygmabt ol.searches {
position:absolute;
z-index:10002;
width:155px;
_width:165px;
padding:7px 7px 3px 7px;
background-color:#ffffff;
border:1px solid #cacaca;
margin:0;
}
#ygma ol.searches li, #ygmabt ol.searches li {
text-align:left;
list-style-type:none;
color:#163780;
margin:0;
padding: 0 0 2px 0;
}
#ygma #ygmapromo ol.searches a, #ygmabt #ygmabtpromo ol.searches a {
font-weight:normal;
color:#163780;
}
#ygma #ygmapromo ol.searches a:hover, #ygmabt #ygmabtpromo ol.searches a:hover {
width:100%;
}
#ygma #ygmapromo-i, #ygmabt #ygmapromo-i {
position:relative;
z-index:10002;
zoom:1;
}
</style><script language=javascript>
if(window.yzq_d==null)window.yzq_d=new Object();
window.yzq_d['OFPLD0oGYzg-']='&U=13h2rtkt7%2fN%3dOFPLD0oGYzg-%2fC%3d650008.13959238.14033549.12832737%2fD%3dHPRM2%2fB%3d6076890%2fV%3d1';
</script><noscript><img width=1 height=1 alt="" src="http://us.bc.yahoo.com/b?P=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc&T=17t8jqecg%2fX%3d1342682750%2fE%3d389132707%2fR%3dymb%2fK%3d5%2fV%3d2.1%2fW%3dH%2fY%3dYAHOO%2fF%3d2676527448%2fH%3dc2VydmVJZD0iNHkwbC5XS0pMR0dYWWFLSTZnOXpXUTVod1BveEFWQUh0bjRBQnRYYyIgc2l0ZUlkPSI0NDY1NTUxIiB0U3RtcD0iMTM0MjY4Mjc1MDQ2NTkyNSIg%2fQ%3d-1%2fS%3d1%2fJ%3dFA208962&U=13h2rtkt7%2fN%3dOFPLD0oGYzg-%2fC%3d650008.13959238.14033549.12832737%2fD%3dHPRM2%2fB%3d6076890%2fV%3d1"></noscript></div><div id="pa"><div id="pa-wrapper"><ul id="pa2-nav" class="sp"><li class="pa1 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=5/SIG=10np9vmbm/*http://www.yahoo.com/" target="_top">Yahoo!</a></li><li class="pa2 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=6/SIG=1107gluf6/*http://mail.yahoo.com?.intl=us" target="_top">Mail</a></li></ul><div id="pa-left" class="sp"></div><ul id="pa-nav" class="sp"><li class="pa3 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=7/SIG=10l2nj3k8/*http://my.yahoo.com" title="My Yahoo!" target="_top">My Yahoo!</a></li><li class="pa4 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=8/SIG=10niob72s/*http://news.yahoo.com" title="Yahoo! News" target="_top">News</a></li><li class="pa5 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=9/SIG=10q40gpus/*http://finance.yahoo.com" title="Yahoo! Finance" target="_top">Finance</a></li><li class="pa6 sp"><a class="sp" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=10/SIG=10pcalhda/*http://sports.yahoo.com" title="Yahoo! Sports" target="_top">Sports</a></li></ul><div id="pa-right" class="sp"></div></div></div></div><div id="yahoo" class="ygmaclr"><div id="ygmabot"> <a id="ygmalogo" href="http://global.ard.yahoo.com/SIG=15of4v3ir/M=650008.13179474.13810954.12691855/D=ymb/S=389132707:HEAD/Y=YAHOO/EXP=1342689950/L=4y0l.WKJLGGXYaKI6g9zWQ5hwPoxAVAHtn4ABtXc/B=NlPLD0oGYzg-/J=1342682750498023/K=DHARfvXptNXfBjvsJrQw.g/A=5857129/R=11/SIG=10q40gpus/*http://finance.yahoo.com" target="_top"><img id="ygmalogoimg" width="246" h...
below is my code to read a web url,
public String getHtml(String urlstr)
{
try {
URL url = new URL(urlstr);
URLConnection conn = url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
System.out.println("now to set cookie: " + cookie);
conn.addRequestProperty("Cookie", cookie);
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
extractCookie(conn);
//PrintWriter file = new PrintWriter(new BufferedWriter(new FileWriter("out.txt")));
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null)
{
//file.println(inputLine);
sb.append(inputLine).append("\n");
}
in.close();
//file.close();
return sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private void extractCookie(URLConnection conn) {
System.out.println("now try to get cookie:");
Map<String, List<String>> fields = conn.getHeaderFields();
for(String key: fields.keySet())
{
List<String> values = fields.get(key);
if("Set-Cookie".equalsIgnoreCase(key))
{
System.out.println(values.size());
for(String v: values)
{
String c = Util.getCookie(v);
this.cookieSet.add(c);
System.out.println(c);
}
//this.cookieMap.p
//cookieSet.add(values);
}
}
}
well, I think this code work fine,
it's in debug mode that eclipse variable did not show full value of a long string that confused me...
( i copied the output from debug mode's variable value....)
later i printed to console/file it seemed fine for the cases i've tested so far...