Created
February 4, 2014 15:48
-
-
Save gusthavosouza/8806210 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 web; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.Serializable; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.bean.ManagedBean; | |
import javax.faces.bean.RequestScoped; | |
import javax.faces.context.ExternalContext; | |
import javax.faces.context.FacesContext; | |
import org.primefaces.event.FileUploadEvent; | |
/** | |
* | |
* @author Gustavo | |
*/ | |
@ManagedBean | |
@RequestScoped | |
public class UploadImageMB implements Serializable { | |
public UploadImageMB() { | |
} | |
private String pastaDoUpload; | |
private static final int BUFFER_SIZE = 6124; | |
public void uploadImage(FileUploadEvent uploadEvent) { | |
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); | |
File file = new File(context.getRealPath("//WEB-INF//files//" + uploadEvent.getFile().getFileName())); | |
try { | |
FileOutputStream fileOut = new FileOutputStream(file); | |
byte[] buff = new byte[BUFFER_SIZE]; | |
int bulk; | |
InputStream inputStream = uploadEvent.getFile().getInputstream(); | |
while (true) { | |
bulk = inputStream.read(buff); | |
if (bulk < 0) { | |
break; | |
} | |
fileOut.write(buff, 0, bulk); | |
fileOut.flush(); | |
} | |
fileOut.close(); | |
inputStream.close(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, | |
"o arquivo falhou!", ""); | |
FacesContext.getCurrentInstance().addMessage(null, error); | |
} | |
String fill = String.valueOf(file); | |
System.out.println(fill); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment