I have a successful ajax that takes an array of urls e.g. "http://ift.tt/1GY1lP5" and POSTs to my generate servlet. I can see the array string when i alert it during the ajax javascript file. I can see that POST to generate servlet occurs from the browser network tab.
The array is passed to my generate serlvet and there I want to
a) view the contents of the array , but i'm not sure how. i have some system.out.printlns but they don't seem to do anything when i run the webpage and click the button.
b) take the contents of the array and (if they are not already a string) then parse them into a java arraylist . a website gave me (rather longwinded) methods to do this where i convert the array to a List (object?) then the List to an ArrayList String
c) lastly i want to feed the Array to my imageController which tries to loop through the arraylist. HOWEVER, it gives me the message "object cannot be converted to string" , indicating that something hasn't worked down the line?
The code is pretty long but i think the problem areas are very distinct (i commented the two suspect areas). If you could take a look that would be amazing. If anything, maybe you could tell me how I can print these values to my screen somehow since my system.out.println thing isn't activating....
Thankyou
Ajax (should be working...)
$(document).ready(function () {
$('#button').click(function () {
var array = [];
$('#sortable2 .selectedItemImg').each(function () {
array.push($(this).attr('src'));
});
alert(array);
$.ajax({
url: 'generate',
type: 'POST',
dataType: 'array',
data: (array),
success: function (data) {
// alert("test44");
}
// send url to user
//
//
}
);
});
return false;
}
);
Servlet
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author J
*/
@WebServlet(name = "ImageGenerationServlet", urlPatterns = {"/generate"})
public class ImageGenerationServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ImageGenerationServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ImageGenerationServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
// THESE PARTS DONT APPEAR TO WORK!! :)
List imageList = Arrays.asList(request.getParameter("button"));
System.out.println(imageList);
ArrayList <String> imageURLs = new ArrayList(Arrays.asList(imageList));
System.out.println(imageURLs);
ImageController imgc = new ImageController(imageURLs);
// 1) retrieve array of urls
// 2) send to imagegenerationcontroller
// 3) send to uploadcontroller
// 4) retrieve and parse the json response to get the imgur url
// 5) response from here to user
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
ImageController (do stuff with the string , except it doesnt appear to have been turned into a string)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
*
* @author J
*/
public class ImageController {
//int roundUp(int numToRound, int multiple)
//{
// if(multiple == 0)
// {
// return numToRound;
// }
//
// int remainder = numToRound % multiple;
// if (remainder == 0)
// return numToRound;
// return numToRound + multiple - remainder;
//}
int roundUp(int numToRound, int multiple) {
return (numToRound+multiple-1) / multiple;
}
public ImageController(ArrayList imageURLs) throws IOException
{
// ArrayList <String> imageURLs = new ArrayList<>();
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//imageURLs.add("C:\\Users\\J\\Desktop\\test5.jpg");
//Integer totalHeight= 300*((Math.round(imageURLs.size()/6)));
//Integer totalHeight = imageURLs.size()*300/4;
//Integer totalHeight = 300*(roundUp(imageURLs.size(), 4));
Integer totalHeight = (roundUp(imageURLs.size(),4))*200;
System.out.println(imageURLs.size());
System.out.println(totalHeight);
// height = numberofentries / 4 rounded up to the nearest multiple of 4
// height = numberofentries rounded up to the nearest 4, divided by 4, times 300px
//Double heightMath= 300*(4*(Math.ceil(Math.abs(imageURLs.size()/4.0))));
//Long heightMath= 300*(long)Math.floor(imageURLs.size() + 1d);
//Integer totalHeight = (int) (double) heightMath;
//if (totalHeight < 300){
// totalHeight = 300;
// }
BufferedImage result = new BufferedImage(
736, totalHeight, //work these out
BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Integer x = 0;
Integer y = 0;
//THIS SAYS "OBJECT CANNOT BE CONVERTED TO STRING!"
// IT SHOULD HAVE BEEN PARSED INTO AN ARRAYLIST<STRING> ALREADY?
for(String imageURL : imageURLs){
BufferedImage bi = ImageIO.read(new File(imageURL));
g.drawImage(bi, x, y, null);
x += 184;
if(x >= result.getWidth()){
x = 0;
y += bi.getHeight();
}
ImageIO.write(result,"png",new File("C:\\Users\\J\\Desktop\\resultimage.jpg"));
}
}
}
Aucun commentaire:
Enregistrer un commentaire