jsf primefaces文件下载乱码

 

package org.primefaces.component.filedownload;

 

import java.io.IOException;

 

import javax.el.ELContext;

import javax.el.ValueExpression;

import javax.faces.component.StateHolder;

import javax.faces.context.FacesContext;

import javax.faces.event.AbortProcessingException;

import javax.faces.event.ActionEvent;

import javax.faces.event.ActionListener;

import javax.servlet.http.HttpServletResponse;

 

import org.primefaces.model.StreamedContent;

 

public class FileDownloadActionListener implements ActionListener, StateHolder {

 

private ValueExpression value;

private ValueExpression contentDisposition;

public FileDownloadActionListener() {}

public FileDownloadActionListener(ValueExpression value, ValueExpression contentDisposition) {

this.value = value;

this.contentDisposition = contentDisposition;

}

 

public void processAction(ActionEvent actionEvent) throws AbortProcessingException {

FacesContext facesContext = FacesContext.getCurrentInstance();

ELContext elContext = facesContext.getELContext();

HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

String contentDispositionValue = contentDisposition != null ? (String) contentDisposition.getValue(elContext) : "attachment";

StreamedContent content = (StreamedContent) value.getValue(elContext);

try {

   String fileName = java.net.URLEncoder.encode(content.getName(), "UTF-8"); 

   response.setCharacterEncoding("UTF-8");

response.setContentType(content.getContentType());

response.setHeader("Content-Disposition", contentDispositionValue + ";filename=" + fileName);

byte[] buffer = new byte[2048];

int length;

while ((length = (content.getStream().read(buffer))) >= 0) {

response.getOutputStream().write(buffer, 0, length);

}

response.setStatus(200);

content.getStream().close();

response.getOutputStream().flush();

facesContext.responseComplete();

}catch (IOException e) {

e.printStackTrace();

}

}

 

public boolean isTransient() {

return false;

}

 

public void restoreState(FacesContext facesContext, Object state) {

Object values[] = (Object[]) state;

 

value = (ValueExpression) values[0];

contentDisposition = (ValueExpression) values[1];

}

 

public Object saveState(FacesContext facesContext) {

Object values[] = new Object[2];

 

values[0] = value;

values[1] = contentDisposition;

return ((Object[]) values);

}

 

public void setTransient(boolean value) {

}

}

 

 

 

 

 

 

jsf源码:

 

 

<table style="width:93%;margin:6px 0px 6px 0px;">

<tr>

<td>

<p:dataTable value="#{fileUploadAction.sfId}" var="fileUpLoad" id="fileID" emptyMessage="没有文件!"> 

   

   <p:columnGroup type="header">

<p:row>

<p:column headerText="文件名称" />

<p:column headerText="下载文件" />

<p:column headerText="移除文件" />

</p:row>

</p:columnGroup>

   <p:column>

<h:outputText value="#{fileUpLoad.fName}" title="文件名称"/>

</p:column>

<p:column>

<h:commandLink title="下载文件" action="#{fileUploadAction.downloadFile(fileUpLoad.fId)}">

<p:graphicImage value="/images/download.gif" />

</h:commandLink>

</p:column>

   <p:column>

<h:commandLink  ajax="true" title="移除文件" actionListener="#{fileUploadAction.doDeleteFile}" update="fileID">

<p:graphicImage value="/images/delete.png" />

<f:param name="fId" value="#{fileUpLoad.fId}" />

</h:commandLink>

</p:column>

 </p:dataTable>

 </td>

</tr>

<tr>

<td>

<p:fileUpload widgetVar="uploader" customUI="true"  image="/images/browse.png" title="上传文件" height="48" width="48" fileUploadListener="#{fileUploadAction.doHandleFileUpload}" mode="advanced"

update="fileID" multiple="true" sizeLimit="1000000">

</p:fileUpload>

<p:commandButton value="文件上传" title="文件上传" onclick="uploader.upload();" 

style="font-weight:bold;color:#616D7E">

</p:commandButton>

</td>

</tr>

</table>

你可能感兴趣的:(primefaces 下载乱码)