InputStream为null的缘故看StreamResult的源:1.if (inputStream == null) { 2. // Find the inputstream from the invocation variable stack 3. inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation)); 4. } 5. 6. if (inputStream == null) { 7. String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " + 8. "Check the <param name=\"inputName\"> tag specified for this action."); 9. LOG.error(msg); 10. throw new IllegalArgumentException(msg); 11. } 路径错误,把下载路径打印出来看是否为空NULL
// ActionContext context = ActionContext.getContext();
// HttpServletRequest request = (HttpServletRequest)context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
FileModel fileModel = new FileModel();
List<Object[]> list = fileService.getFcById(fc.getId());
// HttpServletResponse response = ServletActionContext.getResponse();
String fileName;
if(list.size()==0){
fileName = fileModel.getName(); // �ļ���Ĭ�ϱ�����
ByteArrayInputStream inStream = new ByteArrayInputStream(fileModel.getFileByte());
// ��������
// ��������ĸ�ʽ
getResponse().reset();
getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("application/octet-stream;");
// getResponse().addHeader("Content-Disposition",
// "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8"));
getResponse().addHeader(
"Content-Disposition",
new String(("attachment; name=" + fileName)
.getBytes("UTF-8"), "UTF-8"));
// getResponse().encodeURL(path);
// ѭ��ȡ�����е����
byte[] b = new byte[100];
int len;
try {
OutputStream output=getResponse().getOutputStream();
while ((len = inStream.read(b)) > 0)
output.write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//���ص���������
else if(list.size()==1){
// System.out.println("id:"+list.get(0)[0].toString());
// System.out.println("name:"+list.get(0)[1].toString());
// System.out.println("src:"+list.get(0)[2].toString());
fileModel.setId(list.get(0)[0].toString());
fileModel.setName(list.get(0)[1].toString());
fileModel.setSrc(list.get(0)[2].toString());
fileName = fileModel.getName(); // �ļ���Ĭ�ϱ�����
System.out.println("fileName"+fileName);
String fileBody =fileModel.getSrc();
System.out.println("fileBody"+fileBody);
FileInputStream stream=null;
try{
File file = new File(fileBody);
stream=new FileInputStream(file);
}catch (Exception e) {
e.printStackTrace();
return "error";
}
// ��������
// ��������ĸ�ʽ
getResponse().reset();
getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("application/octet-stream;");
getResponse().setHeader("content-type","text/html;charset=UTF-8");
try {
getResponse().setHeader("Content-Length", String.valueOf(stream.available()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
getResponse().addHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName,"UTF-8"));
// ѭ��ȡ�����е����
byte[] b = new byte[100];
int len=0;
try {
// PrintWriter output = getResponse().getWriter();
OutputStream output=getResponse().getOutputStream();
while ((len = stream.read(b))>0)
output.write(b, 0, len);
output.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//���ش���
else if(list.size()>1){
String[] fileurls = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
fileurls[i] = list.get(i)[2].toString();
}
String formatDate =new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
fileName = "D:/test/" + formatDate + ".zip";
if(fileurls!=null){
if(fileurls.length>0){
File[] files = new File[fileurls.length];
for(int i=0;i<fileurls.length;i++) {
if(fileurls[i] != null) {
File file = new File(fileurls[i].trim());
if(file.exists()) {
files[i] = file;
}
}
}
ZipFileUtil.compressFiles2Zip(files,fileName,is);
}
}
return "downAll";
}
return SUCCESS;
}
if(files != null && files.length >0) {
if(isEndsWithZip(zipFilePath)) {
ZipArchiveOutputStream zaos = null;
try {
File zipFile = new File(zipFilePath);
zaos = new ZipArchiveOutputStream(zipFile);
//Use Zip64 extensions for all entries where they are required
zaos.setUseZip64(Zip64Mode.AsNeeded);
//将每个文件用ZipArchiveEntry封装
//再用ZipArchiveOutputStream写到压缩文件中
for(File file : files) {
if(file != null) {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,file.getName());
zaos.putArchiveEntry(zipArchiveEntry);
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024 * 5];
int len = -1;
while((len = is.read(buffer)) != -1) {
//把缓冲区的字节写入到ZipArchiveEntry
zaos.write(buffer, 0, len);
}
//Writes all necessary data for this entry.
zaos.closeArchiveEntry();
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
if(is != null) {
is.close();
}
}
}
}
zaos.finish();
}catch(Exception e){
throw new RuntimeException(e);
}finally {
try {
if(zaos != null) {
zaos.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
<result name="success" type="stream">
<param name="contentType">${contentTypes}</param>
<param name="inputName">reportStreams</param>
<param name="contentDisposition">${fileNames}</param>
<param name="bufferSize">4096</param>
</result>
<result name="downAll" type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">application/octet-stream</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">inputStream</param>
<param name="contentDisposition">${fileNames}</param>
<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
2. // Find the inputstream from the invocation variable stack
3. inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
4. }
5.
6. if (inputStream == null) {
7. String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
8. "Check the <param name=\"inputName\"> tag specified for this action.");
9. LOG.error(msg);
10. throw new IllegalArgumentException(msg);
11. } 路径错误,把下载路径打印出来看是否为空NULL