没用组件来上传,是用的流来上传的
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="x"%>
<h:form enctype="multipart/form-data">
图片:<x:inputFileUpload id="fileId" value="#{picBean.myfile}" storage="file" onchange="showPic()"/>
<img id="picId" height="30 width="30" />
<script type="text/javascript">
function showPic(){
var file= document.getElementById('addForm:fileId');
var pic=document.getElementById('picId');
pic.src=file.value;
}
</script>
PicBean.java
// 上传图片和类型不能为空
if (myfile != null) {
String fileName = new File(myfile.getName()).getName();
String test = myfile.getContentType();
if (!test.startsWith("image/")) {
wrongpictype = "图片格式不对";
return "";
}
String path=UploaderHelper.uploadFileToDisk(myfile);
this.picnews.setTurl(path);
}
附
UploaderHelper.java
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class UploaderHelper {
public final static String uploadPath="uploadFile";
public static String uploadFileToDisk(UploadedFile myFile)
{
try {
long time = System.currentTimeMillis();
InputStream in = myFile.getInputStream();
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession hs = (HttpSession) ctx.getExternalContext().getSession(true);
String directory = hs.getServletContext().getRealPath("//")+uploadPath;
File fileDir = new File(directory);
if (!fileDir.exists()) {
fileDir.mkdir();
}
String fileName=String.valueOf(Thread.currentThread().getContextClassLoader().hashCode())+"_"+time+myFile.getName().substring(myFile.getName().lastIndexOf("."));
String file = hs.getServletContext().getRealPath("//") + uploadPath+"//"+ fileName;
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[1024 * 2];
int length;
while ((length = in.read(buffer, 0, buffer.length)) > 0) {
out.write(buffer, 0, length);
}
out.flush();
out.close();
return uploadPath+"/"+ fileName;
// return "//" + uploadPath+"//"+ fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String downLoadFile()
{
String blobId=FacesUtils.getRequestParameter("blobFileID");
BlobFile blobVO=blobFileService.getBlobFile(Integer.parseInt(blobId));
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletContext servletContext=FacesUtils.getServletContext();
InputStream s = new java.io.ByteArrayInputStream(blobVO.getFileContent());
BufferedInputStream bis = new BufferedInputStream(s);
String realName=blobVO.getName();
String extendName=null;
try {
String mimeType = null;
int pos = realName.lastIndexOf(".");
if (pos >= 0 && (pos + 1) < realName.length()) {
extendName = realName.substring(pos + 1);
}
if (extendName != null)
mimeType = servletContext.getMimeType(extendName);
if (mimeType == null)
mimeType = "APPLICATION/OCTET-STREAM";
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode(realName, "UTF-8") + "/"");
byte[] data = new byte[1024];
OutputStream out = response.getOutputStream();
int length=0;
do {
length = bis.read(data);
if (length != -1) {
out.write(data, 0, length);
}
} while (length > 0);
out.flush();
out.close();
data = null;
//
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
} catch (IOException ex) {
response.setContentType("text/html");
response.setHeader("Content-Disposition", "");
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException ex) {
}
}
FacesContext.getCurrentInstance().responseComplete();
return null;
}
public String downLoadFileFromDisk()
{
// String blobId=FacesUtils.getRequestParameter("blobFileID");
// BlobFile blobVO=blobFileService.getBlobFile(Integer.parseInt(blobId));
String filepath=FacesUtils.getRequestParameter("filepath");;
String realName=FacesUtils.getRequestParameter("realName");;
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletContext servletContext=FacesUtils.getServletContext();
InputStream s=null;
try {
s = new BufferedInputStream(new FileInputStream(filepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(s);
String extendName=null;
try {
String mimeType = null;
int pos = realName.lastIndexOf(".");
if (pos >= 0 && (pos + 1) < realName.length()) {
extendName = realName.substring(pos + 1);
}
if (extendName != null)
mimeType = servletContext.getMimeType(extendName);
if (mimeType == null)
mimeType = "APPLICATION/OCTET-STREAM";
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode(realName, "UTF-8") + "/"");
byte[] data = new byte[1024];
OutputStream out = response.getOutputStream();
int length=0;
do {
length = bis.read(data);
if (length != -1) {
out.write(data, 0, length);
}
} while (length > 0);
out.flush();
out.close();
data = null;
//
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
} catch (IOException ex) {
response.setContentType("text/html");
response.setHeader("Content-Disposition", "");
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException ex) {
}
}
FacesContext.getCurrentInstance().responseComplete();
return null;
}
}