code_junkie

Why is my image coming out garbled?

Nov 10th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. try {
  4. String customerPath = "\leetest\";
  5.  
  6. // Check that we have a file upload request
  7. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  8.  
  9. if (isMultipart) {
  10. // Create a new file upload handler
  11. ServletFileUpload upload = new ServletFileUpload();
  12.  
  13. // Parse the request
  14. FileItemIterator iter = upload.getItemIterator(request);
  15. while (iter.hasNext()) {
  16. FileItemStream item = iter.next();
  17. String name = item.getFieldName();
  18. if (item.isFormField()) {
  19. // Form field. Ignore for now
  20. } else {
  21. BufferedInputStream stream = new BufferedInputStream(item
  22. .openStream());
  23. if (stream == null) {
  24. LOGGER
  25. .error("Something went wrong with fetching the stream for field "
  26. + name);
  27. }
  28.  
  29. byte[] bytes = StreamUtils.getBytes(stream);
  30. FileManager.createFile(customerPath, item.getName(), bytes);
  31.  
  32. stream.close();
  33. }
  34. }
  35. }
  36. } catch (Exception e) {
  37. throw new UploadException("An error occured during upload: "
  38. + e.getMessage());
  39. }
  40. }
  41.  
  42. public static byte[] getBytes(InputStream src, int buffsize)
  43. throws IOException {
  44. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  45. byte[] buff = new byte[buffsize];
  46. while (true) {
  47. int nBytesRead = src.read(buff);
  48. if (nBytesRead < 0) {
  49. break;
  50. }
  51. byteStream.write(buff);
  52. }
  53.  
  54. byte[] result = byteStream.toByteArray();
  55. byteStream.close();
  56.  
  57. return result;
  58. }
  59.  
  60. public static void createFile(String customerPath, String filename,
  61. byte[] fileData) throws IOException {
  62. customerPath = getFullPath(customerPath + filename);
  63. File newFile = new File(customerPath);
  64. if (!newFile.getParentFile().exists()) {
  65. newFile.getParentFile().mkdirs();
  66. }
  67.  
  68. FileOutputStream outputStream = new FileOutputStream(newFile);
  69. outputStream.write(fileData);
  70. outputStream.close();
  71. }
  72.  
  73. 1 while (true) {
  74. 2 int nBytesRead = src.read(buff);
  75. 3 if (nBytesRead < 0) {
  76. 4 break;
  77. 5 }
  78. 6 byteStream.write(buff);
  79. 7 }
  80.  
  81. 1 while (true) {
  82. 2 int nBytesRead = src.read(buff);
  83. 3 if (nBytesRead < 0) {
  84. 4 break;
  85. 5 } else {
  86. 6 byteStream.write(buff, 0, nBytesRead);
  87. 7 }
  88. 8 }
  89.  
  90. byte[] bytes = StreamUtils.getBytes(stream);
  91.  
  92. public static byte[] getBytes(InputStream src, int buffsize)
Add Comment
Please, Sign In to add comment