Uploading Image on jsf - java

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>

Related

How to create a list of uploaded files in Spring Boot?

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(" ");
}

Spring MVC storing files at RestService

Hi I am struggling in storing files at server I am new to spring MVC. Can anyone point me if I am doing it right or wrong.
What I have to do is take some files (text or binary) and store those in the storage device using Restservice.
public #ResponseBody String storeFiles(#RequestBody List<File> filenames, #PathVariable String dirname)
throws Exception {
// TODO Auto-generated method stub
Gson gson = new Gson();
String jsonList = gson.toJson(storeFiles.storeFilesToHitachi(filenames, dirname));
return jsonList;
}public Map<String, Integer> storeFilesToHitachi(List<File> filenames,String dirname) throws Exception{
Map<String, Integer> resultStored = new HashMap<String, Integer>();
if(checkDirAvailable(dirname) || createDirectoryAtHitachi(dirname)){
resultStored = storeFilenamesAtHitachi(filenames, dirname);
}
return resultStored;
}
public boolean createDirectoryAtHitachi(String dirname){
boolean isDirCreated = false;
try{
httpPutRequest.setHeader(HCPUtils.HTTP_AUTH_HEADER,"HCP "+ sEncodedUserName + ":" + sEncodedPassword);
hitachiURI = constructURLForCreateDir(dirname);
httpPutRequest.setURI(hitachiURI);
HttpResponse httpCreateDirResp = httpClient.execute(httpPutRequest);
int responseCode = httpCreateDirResp.getStatusLine().getStatusCode();
if(responseCode == 201) {
isDirCreated = true;
logger.info("A directory by the name "+ dirname +" has been created" );
}
logger.info(responseCode);
}
catch(Exception e){
logger.error("Unable to create directory:" + dirname + e.getMessage());
}
return isDirCreated;
}
public boolean checkDirAvailable(String dirname){
boolean dirAvailable = false;
try{
httpGetRequest.setHeader(HCPUtils.HTTP_AUTH_HEADER,"HCP "+ sEncodedUserName + ":" + sEncodedPassword);
hitachiURI = constructURLForCheckDir(dirname);
httpGetRequest.setURI(hitachiURI);
HttpResponse httpResponse = httpClient.execute(httpGetRequest);
int respCode = httpResponse.getStatusLine().getStatusCode();
if (respCode == 200){
dirAvailable = true;
logger.info("A directory named "+dirname +" is avaliable");
}
logger.info(respCode);
}
catch(Exception e){
logger.error("An exception occured while checking for "+dirname + e.getMessage());
}
return dirAvailable;
}
public Map<String, Integer> storeFilenamesAtHitachi(List<File> filenames,String dirname){
Map<String,Integer> resultMap = new HashMap<String, Integer>();
try{
File filename=null;
httpPutRequest.setHeader(HCPUtils.HTTP_AUTH_HEADER,"HCP "+ sEncodedUserName + ":" + sEncodedPassword);
Iterator<File> iter = filenames.iterator();
while(iter.hasNext()){
filename = iter.next();
hitachiURI = constructURLForStorFilesAtHitachi(dirname, filename);
httpPutRequest.setURI(hitachiURI);
receivedFile = new FileInputStream(filename);
httpPutRequest.setEntity(new InputStreamEntity(receivedFile, -1));
HttpResponse httpPutResponse = httpClient.execute(httpPutRequest);
int respCode = httpPutResponse.getStatusLine().getStatusCode();
resultMap.put(filename.getName(), respCode);
logger.info(resultMap);
logger.info("Response code is :"+respCode +" while saving " +filename +" in directory " +dirname);
}
}
catch(Exception e){
logger.error("Got the following exception while storing files:" +e.getMessage());
}
return resultMap;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<HTML>
<HEAD>
<TITLE>Error 415--Unsupported Media Type</TITLE>
</HEAD>
<BODY bgcolor="white">
<FONT FACE=Helvetica><BR CLEAR=all>
<TABLE border=0 cellspacing=5><TR><TD><BR CLEAR=all>
<FONT FACE="Helvetica" COLOR="black" SIZE="3"><H2>Error 415--Unsupported Media Type</H2>
</FONT></TD></TR>
</TABLE>
<TABLE border=0 width=100% cellpadding=10><TR><TD VALIGN=top WIDTH=100% BGCOLOR=white><FONT FACE="Courier New"><FONT FACE="Helvetica" SIZE="3"><H3>From RFC 2068 <i>Hypertext Transfer Protocol -- HTTP/1.1</i>:</H3>
</FONT><FONT FACE="Helvetica" SIZE="3"><H4>10.4.16 415 Unsupported Media Type</H4>
</FONT><P><FONT FACE="Courier New">The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.</FONT></P>
</FONT></TD></TR>
</TABLE>
</BODY>
</HTML>
My goal is to take list of files and store in particular directory at server.

How to write PDF to client using JSON

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?

why urlconnection did not read full response

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...

How to get a excel file from request?

I am uploading an excel file from one jsp page. below is the code.
<form action="Upload.jsp" enctype="MULTIPART/FORM-DATA" method=post >
<input type="file" name="filename" />
<input type="submit" value="Upload" />
</form>
But how to get the excel file in the next page(Upload.jsp)?
I was using but getting error in the second line.
InputStream file = request.getInputStream();
POIFSFileSystem myFileSystem = new POIFSFileSystem(file );
Then how to get the excel file from the request?
You are getting a Multipart/form-data on the request from which you need to extract the Part containing your file bytes.
The simplest to do this is to use Apache Commons Fileupload
http://commons.apache.org/fileupload/
Create a FileUploader
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.;
import java.io.;
public class FileUploader
{
private static ServletFileUpload uploader;
private FileUploader()
{
}
public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB)
{
if(uploader == null)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
factory.setRepository(new File(tempDir));
uploader = new ServletFileUpload(factory);
uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
}
return uploader;
}
}
Then use it when processing the request
protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
throws FileSizeLimitExceededException
{
if(!isMultipartRequest(request))
return null;
ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
MultiPartFormData data = new MultiPartFormData();
try
{
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items)
{
if(item.isFormField())
{
data.getParameters().put(item.getFieldName(), item.getString());
}
else
{
String filename = item.getName();
//Internet explorer and firefox will send the file name differently
//Internet explorer will send the entire path to the file name including
//the backslash characters etc ... we should strip it down
//THIS IS HACKY
if(filename.indexOf("\\") != -1)
{
int index = filename.lastIndexOf("\\");
filename = filename.substring(index + 1);
}
if(filename == null || filename.equals(""))
{
//do nothing
}
else
{
String randomFileName = (new RandomGUID()).toString() + getFileExtension(filename);
File uploadFile = new File(uploadDir + File.separator + randomFileName);
item.write(uploadFile);
}
}
}
}
catch(FileSizeLimitExceededException e)
{
throw e;
}
catch(Exception e)
{
e.printStackTrace();
}
return data;
}
For your reference ... MultiPartForm data looks like
import java.util.Hashtable;
import java.util.ArrayList;
public class MultiPartFormData {
private ArrayList<Integer> fids;
private Hashtable<String, String> parameters;
public MultiPartFormData()
{
this.fids = new ArrayList<Integer>();
this.parameters = new Hashtable<String, String>();
}
public ArrayList<Integer> getFids() {
return fids;
}
public void setFids(ArrayList<Integer> fids) {
this.fids = fids;
}
public Hashtable<String, String> getParameters() {
return parameters;
}
public void setParameters(Hashtable<String, String> parameters) {
this.parameters = parameters;
}
}
Well guys, thanks for all the reply. But I have resolved the problem with below process.
Inside JSP:
<form action="/upload.do" enctype="MULTIPART/FORM-DATA" method=post >
<input type="file" name="file" id="file" size=25/>
<input type="submit" value="Upload" />
</form>
Created a form bean: inside that
private FormFile file;
public void setFile(FormFile file) {
this.file = file;
}
public FormFile getFile() {
return file;
}
In action class upload:
FileUploadForm uploadForm = (FileUploadForm) form;
FormFile file = uploadForm.getFile();
InputStream stream = file.getInputStream();
POIFSFileSystem fsFileSystem = new POIFSFileSystem(stream);
//
rest of code for reading the excel
//
Now its working fine.

Categories