文档库 最新最全的文档下载
当前位置:文档库 › Struts2文件预览与下载

Struts2文件预览与下载

Struts2文件预览与下载
Struts2文件预览与下载

Struts2文件预览与下载

1、Jar包列表

2、com.efund.read.action.ReadAction

package com.efund.read.action;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.io.InputStreamReader;

import https://www.wendangku.net/doc/7a6493287.html,.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class ReadAction extends ActionSupport implements ServletRequestAware {

private HttpServletRequest request;

private InputStream inputStream;

private String filename;

public String getFilename() throws Exception {

return URLEncoder.encode(filename,"UTF-8");

}

public void setFilename(String filename) throws Exception{

byte[] buf = filename.getBytes("ISO8859-1");

filename = new String(buf,"UTF-8");

this.filename = filename;

}

public InputStream getInputStream() {

return inputStream;

}

@Override

public void setServletRequest(HttpServletRequest request) { this.request = request;

}

public String readReq(){

String path =

request.getSession().getServletContext().getRealPath("/") +

"file\\request\\Contact.java";

try {

inputStream = new FileInputStream(new File(path));

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

// request.getSession().setAttribute("message", "读取request文件");

return "stream";

}

public String readRes(){

String path =

request.getSession().getServletContext().getRealPath("/") +

"file\\response\\Book.java";

try {

inputStream = new FileInputStream(new File(path));

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

return "stream";

}

public String read(){

//读取request文件

String path =

request.getSession().getServletContext().getRealPath("/") +

"file\\request\\Contact.java";

System.out.println("path-->" + path);

StringBuilder sb = new StringBuilder();

try {

// 字节流

FileInputStream fis = new FileInputStream(path);

// 字符流

InputStreamReader isr = new InputStreamReader(fis,"utf-8");

// 缓冲流

BufferedReader br = new BufferedReader(isr);

String line = null;

//逐行读,每读完一行加

while((line = br.readLine() )!=null) {

sb.append(line);

sb.append("
");

}

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

request.setAttribute("request", sb.toString());

//读取response文件

sb = new StringBuilder();

path = request.getSession().getServletContext().getRealPath("/") + "file\\response\\Book.java";

try {

// 字节流

FileInputStream fis = new FileInputStream(path);

// 字符流

InputStreamReader isr = new InputStreamReader(fis,"utf-8");

// 缓冲流

BufferedReader br = new BufferedReader(isr);

String line = null;

while((line = br.readLine() )!=null) {

sb.append(line);

sb.append("
");

}

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

request.setAttribute("response", sb.toString());

return "result";

}

public String downReq(){

String path =

request.getSession().getServletContext().getRealPath("/") +

"file\\request\\" + filename;

try {

inputStream = new FileInputStream(path);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return "download";

}

public String downRes(){

String path =

request.getSession().getServletContext().getRealPath("/") +

"file\\response\\" + filename;

try {

inputStream = new FileInputStream(path);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return "download";

}

}

3、struts.xml

"https://www.wendangku.net/doc/7a6493287.html,/dtds/struts-2.1.dtd">

/index1.jsp

name="contentDisposition">attachment;filename=${filename}

inputStream

4、web.xml

xmlns="https://www.wendangku.net/doc/7a6493287.html,/xml/ns/javaee"

xmlns:xsi="https://www.wendangku.net/doc/7a6493287.html,/2001/XMLSchema-instance"

xsi:schemaLocation="https://www.wendangku.net/doc/7a6493287.html,/xml/ns/javaee

https://www.wendangku.net/doc/7a6493287.html,/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFi lter

struts2

*.action

5、预览与下载解决方案

访问http://localhost:8080/项目名/index1.jsp

index1.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="https://www.wendangku.net/doc/7a6493287.html,/jsp/jstl/core"prefix="c"%>

My JSP 'index.jsp' starting page

读取或下载文件

request文件response文件

href="read_downReq.action?filename=Contact.java">下载request文件

href="read_downRes.action?filename=Book.java">下载response文件

${requestScope.request}${requestScope.response}

6、预览方案2,frameset标签

访问:http://localhost:8080/项目名

1) index.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="https://www.wendangku.net/doc/7a6493287.html,/jsp/jstl/core"prefix="c"%>

My JSP 'index.jsp' starting page

2) top.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="https://www.wendangku.net/doc/7a6493287.html,/jsp/jstl/core"prefix="c"%>

My JSP 'index.jsp' starting page

${message}

top

3) Request.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="https://www.wendangku.net/doc/7a6493287.html,/jsp/jstl/core"prefix="c"%>

My JSP 'index.jsp' starting page

预览request文件

4)Response.jsp

<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%> <%@taglib uri="https://www.wendangku.net/doc/7a6493287.html,/jsp/jstl/core"prefix="c"%>

My JSP 'index.jsp' starting page

预览response文件

相关文档