Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. package model;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.faces.application.FacesMessage;
  6. import javax.faces.context.ExternalContext;
  7. import javax.faces.context.FacesContext;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.ServletContext;
  10. import javax.servlet.http.Part;
  11.  
  12. public class FileUpload {
  13.  
  14. private final int limit_max_size = 10240000;
  15. private final String limit_type_file = "gif|jpg|png|jpeg|pdf|doc|docx|txt";
  16. private String path_to = "";
  17.  
  18. public FileUpload() {
  19.  
  20. }
  21.  
  22. public String processUpload(Part fileUpload) {
  23. String fileSaveData = "noimages.jpg";
  24.  
  25. try {
  26.  
  27. if (fileUpload.getSize() > 0) {
  28. String submittedFileName = getFilename(fileUpload);
  29. if (checkFileType(submittedFileName)) {
  30. if (fileUpload.getSize() > this.limit_max_size) {
  31. FacesContext.getCurrentInstance().addMessage(null,
  32. new FacesMessage(FacesMessage.SEVERITY_INFO, "File size too large!", ""));
  33. } else {
  34. String currentFileName = submittedFileName;
  35. String extension = currentFileName.substring(currentFileName.lastIndexOf("."),
  36. currentFileName.length());
  37. long nameRadom = Calendar.getInstance().getTimeInMillis();
  38. String newfilename = currentFileName;
  39.  
  40. fileSaveData = newfilename;
  41. String fileSavePath = path_to;
  42.  
  43. try {
  44. byte[] fileContent = new byte[(int) fileUpload.getSize()];
  45. InputStream in = fileUpload.getInputStream();
  46. in.read(fileContent);
  47.  
  48. File fileToCreate = new File(fileSavePath, newfilename);
  49.  
  50. File folder = new File(fileSavePath);
  51. if (!folder.exists()) {
  52. folder.mkdirs();
  53. }
  54. FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
  55. fileOutStream.write(fileContent);
  56. fileOutStream.flush();
  57. fileOutStream.close();
  58. fileSaveData = newfilename;
  59. } catch (IOException e) {
  60. fileSaveData = "noimages.jpg";
  61. }
  62.  
  63. }
  64.  
  65. } else {
  66. fileSaveData = "noimages.jpg";
  67.  
  68. }
  69.  
  70. }
  71. } catch (Exception ex) {
  72. fileSaveData = "noimages.jpg";
  73.  
  74. }
  75. return fileSaveData;
  76. }
  77.  
  78. public String getPath_to() {
  79. return path_to;
  80. }
  81.  
  82. public void setPath_to(String path_to) {
  83. this.path_to = path_to;
  84. }
  85.  
  86. private String getFilename(Part part) {
  87.  
  88. for (String cd : part.getHeader("content-disposition").split(";")) {
  89. if (cd.trim().startsWith("filename")) {
  90. String filename = cd.substring(cd.indexOf('=') + 1).trim().replace(""", "");
  91.  
  92. return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1);
  93.  
  94. }
  95. }
  96. return null;
  97. }
  98.  
  99. private boolean checkFileType(String fileName) {
  100. if (fileName.length() > 0) {
  101. String[] parts = fileName.split("\.");
  102. if (parts.length > 0) {
  103. String extention = parts[parts.length - 1];
  104. return this.limit_type_file.contains(extention);
  105. }
  106. }
  107. return false;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement