<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ page import="java.io.*"%>
<html>
<head>
<meta http-equiv="content-Type" contentType="text/html;charset=gb2312">
<title>上传</title>
</head>
<body>
<%
 try{
  String temp=(String)session.getId();//获得sessionId
  File f1=new File((String)request.getRealPath("photo")+"/",temp);    //获得photo所在的目录,并加上sessionId
  out.println((String)request.getRealPath("photo"));
  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.getRealPath("photo")+"/",filename);                                                                                                    //以文件的名创建另一个文件随机读取
  RandomAccessFile random2=new RandomAccessFile(f2,"rw");                                                                                                 //以文件的名创建另一个文件随机读取写入流
  random.seek(random.length());
  long endPosition=random.getFilePointer();
  int j=1;
  long =endPosition;
  while(<=0&&j<=6){
   --;
   random.seek();
   n=random.readByte();
   if(n=='\n'){
    endPosition=random.getFilePointer();
    j++;
   }                                                                                     //去掉临时文件末尾的6个'\n'字符
  }
  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("文件上传成功!");history.back();</script>
<%
 }catch(Exception e)
 {
  e.printStackTrace();
  out.println("上传文件失败!");
 }
%>以上的代码是在网上找的,可以实现上传功能,但是有一点。他的上传路径是写死的。也就是里面的f1变量,我想说的是,如果我想改一下路径,能做到嘛,代码要如何来改呢?

解决方案 »

  1.   

     我的资源里有个 Java 上传源码 ,下载后即可使用,楼主去看看
      

  2.   

    把上传的处理写在一个servlet中或者struts中,建议使用struts的FormFile来上传,比较简单好用,至于目录文件你完全可以自己定义的
      

  3.   

    直接改下路径就可以了,比如改Phone的地址。
    我还是回水源去。
      

  4.   

    可以改的.比如你想存在项目的文件夹里面的话.File f1=new File((String)request.getRealPath("photo")+"/",temp); 
    File f1=new File(/项目明/文件名); 
    这个是活的 可以随便改? 
      

  5.   

    可以改!除了那句File f1=new File((String)request.getRealPath("photo")+"/",temp); 
    涉及到路径外,别的地并没有涉及到路径的问题啊!
      

  6.   

    到这里下载源码吧,注释全,还可以自定义路径
    jsp上传文件源代码jsp下载文件源码