Struts HTTP 文件下载

这篇博客介绍了如何在Struts框架中实现从指定HTTP地址下载文件的详细过程,通过FileDownloadAction.java代码示例进行讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值