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:param name="paramName" value="paramValue"></jsp:param>其中name为与属性相关联的关键词,value为属性的值。
示例:传常量字符串<param name="userName" value="shilei"/>
示例:传变量 User user=new User("shilei");//User是一个类
&n ......
在sql2000中创建存储过程:
--求两数之和
create proc up_sum @a int ,@b int ,@result int output
as
select @result= @a+@b
调用方法:
...
CallableStatement cstmt=con.prepareCall("exec up_sum ?,?,?");
cstmt.registerOutParameter(3,java.sql.Types.INTEGER);
&nb ......
在jsp中,如果在url中传递中文时会出现乱码,在网上关于这一问题的解决方法五花八门,但都不是很奏效!其实解决方法非常简单: 第一步:编码(以传递的参数为str为例) 在传递数据前将str进行编码Java.net.URLEncoder.encode(str) 第二步:解码 在获取数据端将得到的数据进行解码 new String(str.getBytes("ISO8859_1"))
......
以下属个人想法!也许是对JAVA一见钟情而说!
学过ASP.NET的再去学JSP,那感觉就完全不一样了啊!如果过渡过来了,那还好,没过渡那叫惨啊!两个世界啊!还好;我是渡过了,一心就是JAVA,所以脑子里都是JAVA的理念,开发JSP和开发JAVA应用程序一样,无非JSP多了个WEBROOT。
  ......
pageEncoding
在JSP标准的语法中,如果pageEncoding属性存在,那么JSP页面的字符编码方式就由pageEncoding决定,否则就由contentType属性中的charset决定,如果charset也不存在,JSP页面的字符编码方式就采用默认的ISO-8859-1。
ContentType
ContentType属性指定了MIME类型和JSP页面回应时的字符编码方式。MIME类型的 ......