jsp上传



jsp文件上传目前知道的有三种方式

1.<a>标签

2.action中设置响应头后进行转发

3.action中设置响应头后,把文件加入到响应流后返回

以下为实现代码,框架->struts1

struts-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans >
    <form-bean name="userForm" type="com.yourcompany.struts.form.UserForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      parameter="flags"
      attribute="userForm"
      input="/index.jsp"
      name="userForm"
      path="/user"
      scope="request"
      type="com.yourcompany.struts.action.UserAction"
      cancellable="true" />

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <a href="ziyuan/123.docx" target="_blank">下载文档</a>
    <a href="user.do?flags=loaddown" target="_blank">下载文档</a>
    <a href="user.do?flags=downloadstream" target="_blank">下载d文档</a>
  </body>
</html>

.java文件

/** 
 * MyEclipse Struts
 * Creation date: 05-09-2017
 * 
 * XDoclet definition:
 * @struts.action path="/user" name="userForm" scope="request" validate="true"
 */
public class UserAction extends DispatchAction {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward downloadstream(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
		response.setContentType("application/octet-stream");
		String filedown="ziyuan/123.docx";
		String filedisplay = "12345df6.doc";
		String filenamedisplay = URLEncoder.encode(filedisplay, "UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);
		ServletOutputStream outp = null;
		FileInputStream fo = null;
		try {
			File file = new File("src\123.docx");
			outp = response.getOutputStream();
			fo = new FileInputStream("d:/123.docx");
			byte[] bytes = new byte[1024];
			int len = fo.read(bytes, 0, 1024);
			while(len>0){
				outp.write(bytes, 0, len);
				len = fo.read(bytes, 0, 1024);
			}
			outp.flush();
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(fo!=null){
				fo.close();
			}
			
		}
		return null;
	}
	public ActionForward loaddown(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
		response.setContentType("application/octet-stream");
		String filedown="ziyuan/123.docx";
		String filedisplay = "123456.doc";
		String filenamedisplay = URLEncoder.encode(filedisplay, "UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);
		try {
			RequestDispatcher dispatcher = request.getRequestDispatcher(filedown);
			if(dispatcher!=null){
				dispatcher.forward(request, response);
			}
			response.flushBuffer();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			
		}		
		return null;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值