上传:
MyjspForm mf = (MyjspForm) form// TODO Auto-generated method stub
FormFile fname=mf.getFname()
byte [] fn = fname.getFileData()
OutputStream out = new FileOutputStream("D:\\"+fname.getFileName())
Date date = new Date()
String title = fname.getFileName()
String url = "d:\\"+fname.getFileName()
Upload ul = new Upload()
ul.setDate(date)
ul.setTitle(title)
ul.setUrl(url)
UploadDAO uld = new UploadDAO()
uld.save(ul)
out.write(fn)
out.close()
下载:
DownloadForm downloadForm = (DownloadForm)form
String fname = request.getParameter("furl")
FileInputStream fi = new FileInputStream(fname)
byte[] bt = new byte[fi.available()]
fi.read(bt)
//设置文件是下载还是打开以及打开的方式msdownload表示下载;设置字湖集,//主要是解决文件中的中文信息
response.setContentType("application/msdownloadcharset=gbk")
//文件下载后的默认保存名及打开方式
String contentDisposition = "attachmentfilename=" + "java.txt"
response.setHeader("Content-Disposition",contentDisposition)
//设置下载长度
response.setContentLength(bt.length)
ServletOutputStream sos = response.getOutputStream()
sos.write(bt)
return null
jsp上传文本并显示内容:<input type="file" onchange="onFileSelected(event)">
<textarea id="result"></textarea>
function onFileSelected(event) {
var selectedFile = event.target.files[0]
var reader = new FileReader()
var result = document.getElementById("result")
reader.onload = function(event) {
result.innerHTML = event.target.result
}
reader.readAsText(selectedFile)
}
显示:
<c:import var="data"
url="http://www.example.com/file.txt"
scope="session"/>
<c:out value="${data}"/>
JSP页面部分:<%@ page contentType="text/htmlcharset=GB2312" %>
<%@ page import="tom.jiafei.UpFile" %>
<jsp:useBean id="upFile" class="tom.jiafei.UpFile" scope="session" />
<HTML><BODY><P>选择要上传的文件:<BR>
<FORM action="" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="45">
<BR><INPUT type="submit" name ="g" value="提交">
</FORM>
<% upFile.setRequest(request)
upFile.setSession(session)
%>
<jsp:getProperty name="upFile" property="upFileMessage"/>
<P>如果上传的是图像文件,可单击超链接查看图像:
<BR><A href="show.jsp">查看图像</A>
</BODY></HTML>
处理类:
package tom.jiafei
import java.io.*
import javax.servlet.http.*
public class UpFile
{ HttpServletRequest request
HttpSession session
String upFileMessage=""
public void setRequest(HttpServletRequest request)
{ this.request=request
}
public void setSession(HttpSession session)
{ this.session=session
}
public String getUpFileMessage()
{ String fileName=null
try{ String tempFileName=(String)session.getId()//客户的session的id
File f1=new File("D:/tomcat614/Tomcat 6.0/webapps/file/upfile",tempFileName)
FileOutputStream o=new FileOutputStream(f1)
InputStream in=request.getInputStream()
byte b[]=new byte[10000]
int n
while( (n=in.read(b))!=-1)
{ o.write(b,0,n)
}
o.close()
in.close()
RandomAccessFile random=new RandomAccessFile(f1,"r")
int second=1 //读出f1的第2行,析取出上传文件的名字:
String secondLine=null
while(second<=2)
{ secondLine=random.readLine()
second++
}
//获取第2行中目录符号'\'最后出现的位置
int position=secondLine.lastIndexOf('\\')
//客户上传的文件的名字是:
fileName=secondLine.substring(position+1,secondLine.length()-1)
byte cc[]=fileName.getBytes("ISO-8859-1")
fileName=new String(cc)
session.setAttribute("Name",fileName)//供show.jsp页面使用
random.seek(0)//再定位到文件f1的开头。
//获取第4行回车符号的位置:
long forthEndPosition=0
int forth=1
while((n=random.readByte())!=-1&&(forth<=4))
{ if(n=='\n')
{ forthEndPosition=random.getFilePointer()
forth++
}
}
//根据客户上传文件的名字,将该文件存入磁盘:
File f2= new File("D:/tomcat614/Tomcat 6.0/webapps/file/upfile",fileName)
//File f2= new File("D:/aaa",fileName)
RandomAccessFile random2=new RandomAccessFile(f2,"rw")
//确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
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++
}
}
//将random流指向文件f1的第4行结束的位置:
random.seek(forthEndPosition)
long startPoint=random.getFilePointer()
//从f1读出客户上传的文件存入f2(读取从第4行结束位置和倒数第6行之间的内容)
while(startPoint<endPosition-1)
{ n=random.readByte()
random2.write(n)
startPoint=random.getFilePointer()
}
random2.close()
random.close()
f1.delete()//删除临时文件
upFileMessage=fileName+" Successfully UpLoad"
return upFileMessage
}
catch(Exception exp)
{ if(fileName!=null)
{ upFileMessage=fileName+" Fail to UpLoad"
return upFileMessage
}
else
{ upFileMessage=""
return upFileMessage
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)