当前位置: 技术问答>java相关
关于获取文件名的问题
来源: 互联网 发布时间:2015-04-29
本文导语: 我用 来获取一个文件,但我无法在下一个页面中用getParameter("filename")来得到 文件名,为什么?怎样才可以得到? // 我是过以下几种方法: 当存在ENCTYPE="multipart/form-data"时,下一个页面getParameter()方法失效...
我用
来获取一个文件,但我无法在下一个页面中用getParameter("filename")来得到
文件名,为什么?怎样才可以得到?
//
我是过以下几种方法:
当存在ENCTYPE="multipart/form-data"时,下一个页面getParameter()方法失效。
但如果要上传文件,这句非要不可。
问题:
1。ENCTYPE="multipart/form-data这句是什么意思?
2。那里有比较好的html语言中文教程?
3。怎么可以得到上面说的文件名?
来获取一个文件,但我无法在下一个页面中用getParameter("filename")来得到
文件名,为什么?怎样才可以得到?
//
我是过以下几种方法:
当存在ENCTYPE="multipart/form-data"时,下一个页面getParameter()方法失效。
但如果要上传文件,这句非要不可。
问题:
1。ENCTYPE="multipart/form-data这句是什么意思?
2。那里有比较好的html语言中文教程?
3。怎么可以得到上面说的文件名?
|
使用
ENCTYPE="multipart/form-data和POST時getParameter()方法取不了值只能用
java.util.Enumeration requstStr = mySmartUpload.getRequest().getParameterNames();
其實filename已經不用妳去取了,為何要這么做呢?
ENCTYPE="multipart/form-data和POST時getParameter()方法取不了值只能用
java.util.Enumeration requstStr = mySmartUpload.getRequest().getParameterNames();
其實filename已經不用妳去取了,為何要這么做呢?
|
将网页的request传给下面的class,就可以上传
而且可以解析出文件名,你看一下第一个函数
package expressBeans;
import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.*;
public class upload {
public String[] sourcefile = new String[255];//源文件名
public String objectpath = "c:/";//目标文件目录
public String[] suffix = new String[255];//文件后缀名
public String[] objectfilename = new String[255];//目标文件名
public ServletInputStream sis = null;//输入流
public String[] description = new String[255];//描述状态
public long size = 100*1024;//限制大小
private int count = 0;//已传输文件数目
private byte[] b = new byte[4096];//字节流存放数组
private boolean successful = true;
public void setSourcefile(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException{
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("filename=")) != -1){
s = s.substring(k+10);
k = s.indexOf(""");
s = s.substring(0,k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k+1);
System.out.println(count);
System.out.println(suffix[count]);
// if(canTransfer(count))
System.out.println("trans...");
transferfile(count);
System.out.println("trans...end");
}
if(!successful) break;
}
String url1 = response.encodeURL("/tech-qa-java/uploadresult.html");
response.sendRedirect(url1);
}
public int getCount(){
return count;
}
public String[] getSourcefile(){
return sourcefile;
}
public void setObjectpath(String objectpath){
this.objectpath = objectpath;
}
public String getObjectpath(){
return objectpath;
}
private boolean canTransfer(int i){
suffix[i] = suffix[i].toLowerCase();
//这个是我用来传图片的,各位可以把后缀名改掉或者不要这个条件
// if(sourcefile[i].equals("")||(!suffix[i].equals("gif")&&!suffix[i].equals("jpg")&&!suffix[i].equals("jpeg"))) {description[i]="ERR suffix is wrong";return false;}
//else
return true;
}
private void transferfile(int i){
String x = Long.toString(new java.util.Date().getTime());
try{
objectfilename[i] = x+"."+suffix[i];
System.out.println("filename"+objectfilename[i]);
FileOutputStream out = new FileOutputStream(objectpath+objectfilename[i]);
int a = 0;
int k = 0;
long hastransfered = 0;//标示已经传输的字节数
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("Content-Type:")) != -1) break;
}
sis.readLine(b,0,b.length);
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((b[0]==45)&&(b[1]==45)&&(b[2]==45)&&(b[3]==45)&&(b[4]==45)) break;
out.write(b,0,a);
hastransfered+=a;
if(hastransfered>=size){
description[count] = "ERR The file "+sourcefile[count]+" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if(successful) description[count] = "Right The file "+sourcefile[count]+" has been transfered successfully.";
++count;
out.close();
if(!successful){
sis.close();
File tmp = new File(objectpath+objectfilename[count-1]);
tmp.delete();
}
}
catch(IOException ioe){
description[i]=ioe.toString();
}
}
public upload(){
//可以在构建器里面构建服务器上传目录,也可以在javabean调用的时候自己构建
setObjectpath("D:/bea/wlserver6.1/config/examples/applications/DefaultWebApp/images/");
}
}
而且可以解析出文件名,你看一下第一个函数
package expressBeans;
import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.*;
public class upload {
public String[] sourcefile = new String[255];//源文件名
public String objectpath = "c:/";//目标文件目录
public String[] suffix = new String[255];//文件后缀名
public String[] objectfilename = new String[255];//目标文件名
public ServletInputStream sis = null;//输入流
public String[] description = new String[255];//描述状态
public long size = 100*1024;//限制大小
private int count = 0;//已传输文件数目
private byte[] b = new byte[4096];//字节流存放数组
private boolean successful = true;
public void setSourcefile(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException{
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("filename=")) != -1){
s = s.substring(k+10);
k = s.indexOf(""");
s = s.substring(0,k);
sourcefile[count] = s;
k = s.lastIndexOf(".");
suffix[count] = s.substring(k+1);
System.out.println(count);
System.out.println(suffix[count]);
// if(canTransfer(count))
System.out.println("trans...");
transferfile(count);
System.out.println("trans...end");
}
if(!successful) break;
}
String url1 = response.encodeURL("/tech-qa-java/uploadresult.html");
response.sendRedirect(url1);
}
public int getCount(){
return count;
}
public String[] getSourcefile(){
return sourcefile;
}
public void setObjectpath(String objectpath){
this.objectpath = objectpath;
}
public String getObjectpath(){
return objectpath;
}
private boolean canTransfer(int i){
suffix[i] = suffix[i].toLowerCase();
//这个是我用来传图片的,各位可以把后缀名改掉或者不要这个条件
// if(sourcefile[i].equals("")||(!suffix[i].equals("gif")&&!suffix[i].equals("jpg")&&!suffix[i].equals("jpeg"))) {description[i]="ERR suffix is wrong";return false;}
//else
return true;
}
private void transferfile(int i){
String x = Long.toString(new java.util.Date().getTime());
try{
objectfilename[i] = x+"."+suffix[i];
System.out.println("filename"+objectfilename[i]);
FileOutputStream out = new FileOutputStream(objectpath+objectfilename[i]);
int a = 0;
int k = 0;
long hastransfered = 0;//标示已经传输的字节数
String s = "";
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((k = s.indexOf("Content-Type:")) != -1) break;
}
sis.readLine(b,0,b.length);
while((a = sis.readLine(b,0,b.length)) != -1){
s = new String(b,0,a);
if((b[0]==45)&&(b[1]==45)&&(b[2]==45)&&(b[3]==45)&&(b[4]==45)) break;
out.write(b,0,a);
hastransfered+=a;
if(hastransfered>=size){
description[count] = "ERR The file "+sourcefile[count]+" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if(successful) description[count] = "Right The file "+sourcefile[count]+" has been transfered successfully.";
++count;
out.close();
if(!successful){
sis.close();
File tmp = new File(objectpath+objectfilename[count-1]);
tmp.delete();
}
}
catch(IOException ioe){
description[i]=ioe.toString();
}
}
public upload(){
//可以在构建器里面构建服务器上传目录,也可以在javabean调用的时候自己构建
setObjectpath("D:/bea/wlserver6.1/config/examples/applications/DefaultWebApp/images/");
}
}