struts文件下载,文件来源格式如http://localhost:8080/test/11.jpg
FileDownloadAction.java代码如下:
package com.hsinghsu.test.action;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
private static final long serialVersionUID = 3348881101306356364L;
private String inputPath;// 输入文件流路径,如http://localhost:8080/test/11.jpg
public String down() throws Exception{
if(null == getTargetFile()){
System.out.println("DOWN NOT FOUND");
return "fileNotFound";
}
return SUCCESS;
}
/**
* 定义一个返回InputStream的方法, 该方法将作为被下载文件的入口, 且需要配置stream类型结果时指定inputName参数,
*
* @return
* @throws Exception
*/
public InputStream getTargetFile() throws Exception {
System.out.println("inputPath01:" + inputPath);
URL url = null;
try {
url = new URL(inputPath);
} catch (MalformedURLException e1) {
e1.printStackTrace();
System.out.println("Can not get the url:" + inputPath);
}
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
return inStream;
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("error file not found:" + inputPath);
return null;
}
}
/**
* 原文件URL路径
*
* @param value
*/
public void setInputPath(String value) {
inputPath = value;
}
/**
* struts配置属性fileName 用户保存文件的文件名
*
* @return
*/
public String getFileName() {
System.out.println("Get FileName:" + getFileNameFromUrl(inputPath));
return getFileNameFromUrl(inputPath);
}
/**
* 根据原文件URL获取文件名
*
* @param url
* @return
*/
private String getFileNameFromUrl(String url) {
String name = new Long(System.currentTimeMillis()).toString() + ".X";
int index = url.lastIndexOf("/");
if (index > 0) {
name = url.substring(index + 1);
if (name.trim().length() > 0) {
return name;
}
}
return name;
}
}
struts配置文件struts.xml代码如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="hsing" extends="struts-default">
<action name="download" class="com.hsinghsu.test.action.FileDownloadAction" method="down">
<!-- <param name="inputPath">/files/112.zip</param> --><!-- 指定被下载资源的位置 -->
<result name="success" type="stream">
<!-- <param name="contentType">"${contentType}"</param> --><!-- 指定下载文件的文件类型 -->
<param name="inputName">targetFile</param><!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
<param name="contentDisposition">attachment;filename="${fileName}"</param><!-- 用户以附件的形式下载文件,指定getFileName方法返回文件名 -->
<param name="bufferSize">4096</param><!-- 指定下载文件的缓冲大小 -->
</result>
<result name="fileNotFound">/jsp/error.jsp</result>
</action>
</package>
</struts>
mydownload.jsp代码如下:<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>下载</title>
</head>
<body>
下载跨域文件:
<a href="http://localhost:8686/testDownload2/download.action?inputPath=http://localhost:8080/test/11.jpg">下载jpg文件</a>
<a href="http://localhost:8686/testDownload2/download.action?inputPath=http://localhost:8080/test/11.zip">下载zip文件</a>
<a href="http://localhost:8686/testDownload2/download.action?inputPath=http://localhost:8080/test/11.doc">下载doc文件</a>
</body>
</html>