【ZT】Jsp/Servlet:实现文件上传与下载【二】
来源:互联网 发布:mac如何强制关机 编辑:程序博客网 时间:2024/06/05 03:50
4.测试
fileupload.jsp不变,accept.jsp修改如下:
<html>
<head>
<%@ page language="java" import="java.io.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>The real file</title>
</head>
<body>
<%try{
//use sessionid to create a temp file.
String tempFileName=(String)session.getId();
//create the temp file.
File temp=new File("d:/temp",tempFileName);
FileOutputStream o=new FileOutputStream(temp);
if(request.getContentLength()>297){
//write the upload content to the temp file.
InputStream in=request.getInputStream();
byte b[]=new byte[1024];
int n;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
o.close();
in.close();
//read the temp file.
RandomAccessFile random=new RandomAccessFile(temp,"r");
//read Line2 to find the name of the upload file.
int second=1;
String secondLine=null;
while(second<=2){
secondLine=random.readLine();
second++;
}
//get the last location of the dir char.'\\'.
int position=secondLine.lastIndexOf('\\');
//get the name of the upload file.
String fileName=secondLine.substring(position+1,secondLine.length()-1);
//relocate to the head of file.
random.seek(0);
//get the location of the char.'Enter' in Line4.
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
forth++;
}
}
File realFile=new File("d:/temp",fileName);
RandomAccessFile random2=new RandomAccessFile(realFile,"rw");
//locate the end position of the content.Count backwards 6 lines.
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6)){
mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
//locate to the begin of content.Count for 4 lines's end position.
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//read the real content and write it to the realFile.
while(startPoint<endPosition-1){
n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
//delete the temp file.
temp.delete();
out.print("File upload success!");
}
else{
out.print("No file!");
}
}
catch(IOException e){
out.print("upload error.");
e.printStackTrace();
}
%>
</body>
</html>
(注:如果文件名是中文,会出现乱码。)
5.文件下载
Jsp内置对象response调用方法getOutputStream()可以获取一个指向客户的输出流,服务器将文件写入这个流,然后可下载此文件。
- 【ZT】Jsp/Servlet:实现文件上传与下载【二】
- 【ZT】Jsp/Servlet:实现文件上传与下载【一】
- 【ZT】Jsp/Servlet:实现文件上传与下载【三】
- Jsp/Servlet:实现文件上传与下载
- jsp+servlet实现文件上传与下载
- Jsp/Servlet:实现文件上传与下载
- Jsp/Servlet:实现文件上传与下载
- Jsp/Servlet:实现文件上传与下载
- Jsp/Servlet:实现文件上传与下载
- Servlet和Jsp实现原生的上传与下载文件
- 使用jsp/servlet简单实现文件上传与下载
- servlet+jsp实现的文件上传与下载
- 用JSP+servlet实现文件的上传与下载
- 用JSP+servlet实现文件的上传与下载
- JSP+Servlet实现文件的上传与下载
- 使用jsp/servlet实现文件上传与下载
- JSP/Servlet文件上传与下载
- servlet 实现 文件的上传与下载
- 【ZT】解决apache Web服务器网页乱码
- 【ZT】Eclipse Tomcat插件的配置, 及 Tomcat 的配置 ( 谁来教我?)
- 【ZT】Mysql保存word,jpg
- HTML供文件上传功能浏览功能的元素是<input/>的file类型
- 【ZT】Jsp/Servlet:实现文件上传与下载【一】
- 【ZT】Jsp/Servlet:实现文件上传与下载【二】
- 【ZT】Jsp/Servlet:实现文件上传与下载【三】
- java目录文件说明
- 【ZT】Struts 2中实现文件下载(修正中文问题)
- 【ZT】jsp文件下载,解决中文名称乱码
- 【ZT】使用JQuery的each
- 使用commons-fileupload实现文件(doc、pdf)上传,提取表单参数并解决中文乱码
- Packet for query is too large (1825258 > 1048576)
- 【ZT】DOS查找特定字符,并输出到EXCEL方法