基于SSH框架下的实现web网页文件下载的功能以及解决下载文件,文件名为中文时无法显示的问题。

        最近在做一个关于定制自定义问卷的系统,在后台管理那,涉及了一个关于对系统操作的操作文档说明书的下载功能,在制作这个功能的过程中,遇到了一些问题,因此记录下来。

        由于项目用的是SSH框架来实现的,里面就涉及了struct的相关问题,在这里查阅了相关资料,最为靠谱的是直接看去官网,看官方的介绍,我把他给下载了

在文件夹中搜stream。

contentType - the stream mime-type as sent to the web browser (default = text/plain).
contentLength - the stream length in bytes (the browser displays a progress bar).
contentDisposition - the content disposition header value for specifying the file name (default = inline, values are typically attachment;filename="document.pdf".
inputName - the name of the InputStream property from the chained action (default = inputStream).
bufferSize - the size of the buffer to copy from input to output (default = 1024).
allowCaching - if set to 'false' it will set the headers Pragma and Cache-Control to no-cahce, and prevent client from caching the content (default = true).
contentCharSet - if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

看开之后可以看到一些相关属性,这些属性根据自己的需求,写在action类中,当然如果下载功能使用的多的话,可以封装到类当中,在这给出我自己的演示。

download.aciton类

package com.dianxin.tp.web.action.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

public class DownloadAction {

	// 下载操作文档
	public String dealWordDownload() throws UnsupportedEncodingException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/download");
		File file = new File(path + "//投票系统后台操作指南.docx");
		try {
			fileStream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.print("wenjianming" + file.getName());
		// String name = URLEncoder.encode(file.getName(), "utf-8");
		String name = new String(file.getName().getBytes(), "ISO-8859-1");// 解决中文名问题无法再现
		contentDisposition = "attachment;filename=" + name;
		return "dealWordDownloadSUCCESS";
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public InputStream getFileStream() {
		return fileStream;
	}

	public void setFileStream(InputStream fileStream) {
		this.fileStream = fileStream;
	}

	private String contentType;

	private String contentDisposition;

	private InputStream fileStream;

}

jsp页面

<form action="${pageContext.request.contextPath }/download_dealWordDownload.action">
			<input type="submit" value="操作文档下载" />
		</form>

struct相关配置

<action name="download_*" class="downloadAction" method="{1}">
			<result name="dealWordDownloadSUCCESS" type="stream">
                <param name="inputName">fileStream</param>
                <param name="bufferSize">1024</param>
            </result>
		</action>

        在解决下载文件的时候,曾经出现过中文名字的时候为空白,也出现过乱码等情况,其实这些无非都是关于java编码或者web或服务器编码的问题,我在服务器和web都设置好了编码,但是问题还是出现了,同时也查阅了大量的资料,无非都是在获取文件名字那里做编码处理,奇怪的是,网上的资料大多数都是说将文件名的编码设置为utf-8,但是经过我的测试,然而并没有什么用,丝毫改不变不了显示这问题,最后,还是找到了一些相关的资料,才能明白,不是设置utf-8,而是设置为ISO-8859-1。

         在百科是这样解释的,ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号。而http协议的header中只支持 ASCII,详细的还需要去了解一下http协议的编码和解码的知识。

猜你喜欢

转载自blog.csdn.net/weixin_40295575/article/details/81503195