<%@ 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变量,我想说的是,如果我想改一下路径,能做到嘛,代码要如何来改呢?
<%@ 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变量,我想说的是,如果我想改一下路径,能做到嘛,代码要如何来改呢?
解决方案 »
- 创建WEB工程用tomcat project和用dynamic web project有什么区别吗
- 当前页转向后为什么原页中的代码仍然被执行?
- jsp页面里边的按钮调用外部的exe程序的方法??
- 设计一个奇怪的小程序, 遇到一些问题, 求助高手们
- Csdn 没有的板块,只有这里发了,关于 Adobe Document Server 6.0。
- 如何删除相对于Web根目录的文件?
- 字符串截取问题
- 同一个JSP页面Ext.onReady 和wondow.onload那个先执行?
- 学习tomcat例子程序遇到的问题 ,关于jsp中 exception的处理请大家指点;分不够再加
- java interface在servlet中长期的疑惑
- jsp+mysql分级管理的人力资源管理系统
- <display:table>标签中可以实现换行跨列吗?
我还是回水源去。
File f1=new File(/项目明/文件名);
这个是活的 可以随便改?
涉及到路径外,别的地并没有涉及到路径的问题啊!
jsp上传文件源代码jsp下载文件源码