1 43 package net.jforum.view.forum.common; 44 45 import java.io.BufferedInputStream ; 46 import java.io.FileOutputStream ; 47 48 import net.jforum.util.legacy.commons.fileupload.FileItem; 49 50 54 public class UploadUtils 55 { 56 private FileItem item; 57 private String extension = ""; 58 59 public UploadUtils(FileItem item) 60 { 61 this.item = item; 62 } 63 64 public String getExtension() 65 { 66 if (this.extension == null || this.extension.equals("")) { 67 this.extension = this.item.getName().substring(this.item.getName().lastIndexOf('.') + 1); 68 } 69 70 return this.extension; 71 } 72 73 public void saveUploadedFile(String filename) throws Exception 74 { 75 BufferedInputStream inputStream = null; 76 FileOutputStream outputStream = null; 77 78 try { 79 inputStream = new BufferedInputStream (this.item.getInputStream()); 80 outputStream = new FileOutputStream (filename); 81 82 int c = 0; 83 byte[] b = new byte[4096]; 84 while ((c = inputStream.read(b)) != -1) { 85 outputStream.write(b, 0, c); 86 } 87 } 88 finally { 89 if (outputStream != null) { 90 outputStream.flush(); 91 outputStream.close(); 92 } 93 94 if (inputStream != null) { 95 inputStream.close(); 96 } 97 } 98 } 99 } 100 | Popular Tags |