jsp文件上传

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件上传</title>
</head>

<%
  try{
   String temp =(String)session.getId();
   File f1=new File((String)request.getSession().getServletContext().getRealPath("upload")+"/",temp);    //获得上传文件所在的目录录,并加上sessionId
   out.println(f1);
   FileOutputStream o=new FileOutputStream(f1);                         //文件输出流指向上传文件所在路径
   out.println(o);
   InputStream in=request.getInputStream();                             //从客户端获得文件输入流
   int n;
   byte b[]=new byte[10000000];//设置缓冲数组的大小
   while((n=in.read(b))!=-1){
   o.write(b,0,n);                                                  //将数据从输入流读入到缓冲数组然后再从缓冲数组写入到文件中
   }
o.close();
in.close();     //关闭输入流和文件输出流


  RandomAccessFile random=new RandomAccessFile(f1,"r");              //文件随机读取写入流
  int second=1;
  String secondLine=null;
  while(second<=2){
   secondLine=random.readLine();//读入临时文件名
   second++;
  }
  int position=secondLine.lastIndexOf('\\');
  String filename=new String((secondLine.substring(position+1,secondLine.length()-1)).getBytes("iso-8859-1"),
  "gb2312");//去掉临时文件名中的sessionId,获得文件名,并用iso-8859-1编码,避免出现中文乱码问题

  random.seek(0);
  long forthEnPosition=0;
  int forth=1;
  while((n=random.readByte())!=1&&forth<=4){
   if(n=='\n'){
    forthEnPosition=random.getFilePointer();
    forth++;
   }//去掉临时文件开头的4个'\n'字符
  }
 
  File f2=new File((String)request.getSession().getServletContext().getRealPath("upload")+"/",filename);
  //以文件的名创建另一个文件随机读取                                                                                                
  RandomAccessFile random2=new RandomAccessFile(f2,"rw");                                                                                                
  random.seek(random.length());
  long endPosition=random.getFilePointer(); //以文件的名创建另一个文件随机读取写入流
  int j=1;
  long mark=endPosition;
  while(mark<=0&&j<=6){  //去掉临时文件末尾的6个'\n'字符
   mark--;
   random.seek(mark);
   n=random.readByte();
   if(n=='\n'){
    endPosition=random.getFilePointer();
    j++;
   }        
  }
  random.seek(forthEnPosition);
  long startPosition=random.getFilePointer();
  while(startPosition<endPosition-1){//将临时文件去掉头尾后写入到新建的文件中
   n=random.readByte();
   random2.write(n);                                                 
   startPosition=random.getFilePointer();
  }
  random2.close();
  random.close();
  f1.delete();
%>

<script language="javascript">alert("文件上传成功!");</script>
<%
} catch (Exception e) {
e.printStackTrace();
out.println("上传文件失败!");
}
%>
</body>
</html>

posted on   Msea  阅读(131)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端

导航

< 2011年12月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示