jsp上传

来源:互联网 发布:python 安装numpy 编辑:程序博客网 时间:2024/05/20 17:07


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 stubresponse.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 exceptione.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 stubresponse.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 exceptione.printStackTrace();}finally{}return null;}}



0 0
原创粉丝点击