`

一天工作总结4.26

    博客分类:
  • SSH
阅读更多

我擦,今天搞了许多!

 

首选弄了个struts的框架,当然是在网上找的。

package org.usc.file;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class DownloadAction extends ActionSupport
{
	private static final long serialVersionUID = 6329383258366253255L;
	private String fileName;
	private String fileRealName;
	ActionContext context = ActionContext.getContext();    
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);    
    HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);    
    Map session = context.getSession();    

	public void setFileName()
	{
		// 得到请求下载的文件名
		String fname = ServletActionContext.getRequest().getParameter("name");
		String frealname = ServletActionContext.getRequest().getParameter("realname");
		try
		{
			/*
			 * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
			 * 这里使用request.setCharacterEncoding解码无效.
			 * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
			 */
			fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
			frealname= new String(frealname.getBytes("ISO-8859-1"), "UTF-8");
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		this.fileName = fname;
		this.fileRealName = frealname;
		System.out.println(fileName);
		System.out.println(fileRealName);
	}

	/*
	 * @getFileName 此方法对应的是struts.xml文件中的: <param
	 * name="contentDisposition">attachment;filename="${fileName}"</param>
	 * 这个属性设置的是下载工具下载文件时显示的文件名, 要想正确的显示中文文件名,我们需要对fileName再次编码
	 * 否则中文名文件将出现乱码,或无法下载的情况
	 */
	public String getFileName() throws UnsupportedEncodingException
	{

		fileRealName = new String(fileRealName.getBytes(), "ISO-8859-1");

		return fileRealName;
	}
	
	/*
	 * @getDownloadFile 此方法对应的是struts.xml文件中的: <param
	 * name="inputName">downloadFile</param> 返回下载文件的流,可以参看struts2的源码
	 */
	public InputStream getDownloadFile()
	{
		this.setFileName();
		String path=request.getRealPath("\\");
		File file=new File(path+fileName);
		int fileSize=-1;
		if(file.exists()){
			fileSize=(int) file.length();
			System.out.println("文件存在"+file.getPath()+"  "+fileSize);
			}else
				System.out.println("文件不存在"+file.getPath());
				
		response.setContentLength(fileSize);
		return ServletActionContext.getServletContext().
		getResourceAsStream("/"+"/" 
				+ fileName);
	}

	@Override
	public String execute() throws Exception
	{
		return SUCCESS;
	}
}

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="99999999999" /><!--  这里设置大于2M的文件上传下载-->

	<constant name="struts.i18n.encoding" value="utf8" />
	<package name="file" namespace="/" extends="struts-default">
		<action name="showUpload">
			<result>/upload.jsp</result>
		</action>
		<action name="upload" class="org.usc.file.UploadAction">
			<result name="input">/upload.jsp</result>
<!--			<result name="success">/upload_success.jsp</result>-->
			<result name="success">/download.jsp</result>
			<interceptor-ref name="fileUpload">
			
			</interceptor-ref>
			
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>

		<action name="download" class="org.usc.file.DownloadAction">
			<result name="success" type="stream">
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<param name="inputName">downloadFile</param>
			</result>
		</action>
	</package>
	
</struts>

 首先是要做一个struts版的文件服务器,提供下载用!

 

 

然后用一个多线程,断点续传的代码下载这个struts链接,发现我擦,下载不了。

 

故而debug了半天,发现(红色字体)struts默认的IOoutputstream没有把他传的文件大小写到http报的头上,所以断点续传代码无法获得他的大小,所以无法计算。

后来就行了

 

 

 

 

2.(紫色字体)String path=request.getRealPath("\\");是得到该jsp文件的根目录

 ActionContext context = ActionContext.getContext();   
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);   
    HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);   
    Map session = context.getSession();  

这些是得到环境里的那7个属性

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics