On 20 April 2011 08:56, kevin_vdb6 <[email protected]> wrote:
>
> Hello,
>
> I am develloping a systemtray application that sends parameters of a record
> to a servlet, however this is working perfectly with files smaller than 1mb
> but if i try to send larger files it crashes.
Unless you actually provide details of the "crash", it make debugging
much harder.
> Im using Swing as client. (the
> problem is at the Attachment parameter) I've been searching a few days for
> this but i couldn't find an answer, any help would be highly appreciated!
>
> Code client:
> ActionListener sendTask = new ActionListener() {
> @Override
> public void actionPerformed(ActionEvent actionEvent) {
> String request = "http://localhost:8080/rest/addTask";
>
> HttpClient client = new HttpClient();
> PostMethod method = new PostMethod(request);
>
> //conversion blob
> File file = new File(txtUpload.getText());
> try {
> byte[] blob = getBytesFromFile(file);
> String blobje = new String(blob,"ISO-8859-1");
>
> method.addParameter("description",
> txtOmschrijving.getText());
> method.addParameter("attachment", blobje);
> method.addParameter("contentType", new
> MimetypesFileTypeMap().getContentType(file));
> method.addParameter("fileName",file.getName());
> } catch (IOException e) {
> e.printStackTrace(); //To change body of catch statement
> use File | Settings | File Templates.
> }
>
>
> // Send POST request
> try {
> int statusCode = client.executeMethod(method);
> System.out.println(statusCode);
>
> InputStream rstream = null;
>
> rstream = method.getResponseBodyAsStream();
>
> BufferedReader br = new BufferedReader(new
> InputStreamReader(rstream));
>
> String line;
>
> while ((line = br.readLine()) != null) {
> System.out.println(line);
> }
>
>
>
> br.close();
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> txtOmschrijving.setText("");
> txtUpload.setText("");
>
> }
> };
>
>
> public static byte[] getBytesFromFile(File file) throws IOException {
> InputStream is = new FileInputStream(file);
>
> // Get the size of the file
> long length = file.length();
>
> // You cannot create an array using a long type.
> // It needs to be an int type.
> // Before converting to an int type, check
> // to ensure that file is not larger than Integer.MAX_VALUE.
> if (length > Integer.MAX_VALUE) {
> // File is too large
> }
>
> // Create the byte array to hold the data
> byte[] bytes = new byte[(int)length];
>
> // Read in the bytes
> int offset = 0;
> int numRead = 0;
> while (offset < bytes.length
> && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
> offset += numRead;
> }
>
> // Ensure all the bytes have been read in
> if (offset < bytes.length) {
> throw new IOException("Could not completely read file
> "+file.getName());
> }
>
> // Close the input stream and return bytes
> is.close();
> return bytes;
> }
>
>
>
>
> Code on servlet:
> public class AddTaskService extends HttpServlet {
> public void doPost(HttpServletRequest request, HttpServletResponse
> response)
> throws ServletException, IOException {
>
> String description = request.getParameter("description");
> String attachment = request.getParameter("attachment");
> String fileName = request.getParameter("fileName");
> String contentType = request.getParameter("contentType");
>
> //parsing String to Blob
> byte[] blobbinarie = attachment.getBytes("ISO-8859-1");
> InputStream stream = new ByteArrayInputStream(blobbinarie);
>
>
> Blob blobje = Hibernate.createBlob(stream);
>
> //getting TaskController
> ServletContext context = getServletContext();
> WebApplicationContext applicationContext =
> WebApplicationContextUtils.getWebApplicationContext(context);
> TaskController taskController = (TaskController)
> applicationContext.getBean("taskController");
> Date today = Calendar.getInstance().getTime();
> Task task = new Task(description,blobje,today,contentType,fileName);
>
> //try to add new Task into Database
> taskController.createTask(task);
>
> }
> }
>
>
> Thanks in advance,
> Kevin
> --
> View this message in context:
> http://old.nabble.com/Posting-%22large%22-file-with-PostMethod-tp31439133p31439133.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]