JSP文件下载、上传
JSP文件下载-----------------------------------------------------------------
<%@ page contentType="text/html; charset=GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%
String path = request.getParameter("path") ;
if(path==null || "".equals(path)){
out.write("<mce:script type="text/javascript"><!--
") ;
out.write("alert('要下载的文件不存在');") ;
out.write("history.go(-1);") ;
out.write("
// --></mce:script>") ;
}
String realPath = "c:/myfile/正文.doc" ;
File file = new File(realPath) ;
if(!file.exists()){
out.write("<mce:script type="text/javascript"><!--
") ;
out.write("alert('要下载的文件不存在');") ;
out.write("history.go(-1);") ;
out.write("
// --></mce:script>") ;
}else{
long l = file.length() ;
InputStream in = new FileInputStream(file) ;
String fs = file.getName() ;
if(in!=null){
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+new String(fs.getBytes("GBK"),"iso8859-1"));
//以上输出文件元信息
response.setContentLength((int)l); //设置输入文件长度
byte[] b = new byte[2048];
int len = 0;
while((len=in.read(b)) >0)
response.getOutputStream().write(b,0,len); //向浏览器输出
in.close(); //关闭文件输入流
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
}
%>
文件上传----------------------------------------------------------------------
提交页面-------------
<form method="POST" enctype="multipa
相关文档:
1、如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含纯HTML:
但是如果data.inc中包含JSP CODE ,我们可以使用:
2、如何执行一个线程安全的JSP?
只需增加如下指令
3、JSP如何处理HTML FORM中的数据?
通过内置的request对象即可,如下:
String item = request.getParameter("item");
int howM ......
(1)利用<jsp:param name="paramName" value="paramValue"></jsp:param>其中name为与属性相关联的关键词,value为属性的值。
示例:传常量字符串<param name="userName" value="shilei"/>
示例:传变量 User user=new User("shilei");//User是一个类
&n ......
1.request对象
客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。
序号 方 法 & ......
找下面写法。
注意,加单引号,第2个百分号之前没有空格,而在jsp中调用的话要空格。
You may be get some help from the code below.
<%
String str="str";
%>
<script>
function accessVar(){
var varStr='<%=str%>';
alert(varStr);// here will diplay ......
相同点:
如果被包含进去的文件里面有css文件的话,包含的页面会受到被包含文件的css样式的影响!
不同点:
<%@ include
file="date.jsp" %>
<jsp:include
page="date.jsp" flush="true"/>
include
编译指令是在
JSP
程序 ......