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
相关文档:
在项目中,我们经常遇到需要在jsp页面切换中传递中文字符。这主要有两种方式。
URL方式,例如:http://website/test1.jsp?act=add&type=苹果¶m=%20D%20B
FORM方式,例如:
<form name=test mehtod="post">
<input type=hidden name=text2 value="中文">
<input type=t ......
在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 ......
上网看了好多,就这一篇解决了我的问题,现转,我的项目用的是MVC架构,有统一的控制器,转向不同的Action,上网看了好多,大部分是说加入request.setCharacterEncoding("utf-8");但是经过我的试验,这只有在利用JSP处理表单传输数据时才可用,在我的东西中无法解决,后来发现还是用FilterChain好,呵呵,重点还是在request ......
在WEB应用中,如果使用jsp作为view层的显示模板,都会被空格/空换行问题所困扰.
这个问题当年也困扰了我比较长的时间.因为在jsp内使用的EL标签和其他标签时,会产生大量的空格和换行符.例如:
------- start ----------
<c:choose>
<c:when test="${fn:length(mainPageList)>1&}&q ......