如何获取file控件的值!(在线等待)
使用file控件时,但是没有点取该控件时,此时该控件的值等于什么呢? 问题点数:20、回复次数:7Top
1 楼qiming_fawcom(卧底神探)回复于 2002-06-19 09:11:31 得分 10
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;
public class upload
{
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";
String file1="";
String ext="";
private String getFileName(String s)
{
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}
public void setUploadDirectory(String s)
{
uploadDirectory = s;
}
public void setContentType(String s)
{
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}
public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}
public void getfile(String filename01)
{
file1=filename01;
}
public String uploadFile(HttpServletRequest req) throws ServletException, IOException
{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
String filename01=uploadFile(req.getInputStream());
return filename01;
}
public String uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
String line;
//得到文件名
while((line = readLine//Reads the input stream, one line at a time.
(Linebyte, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = line.indexOf("filename=");
if(i >= 0){
line = line.substring(i + 10);
if((i = line.indexOf("\"")) > 0)
line = line.substring(0, i);
break;
}
}
filename = line;
if(filename != null && !filename.equals("\""))
{
filename = getFileName(filename);
file1=file1+filename.substring(filename.indexOf("."));
String sContentType = readLine(Linebyte,ai,servletinputstream,CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
//File(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.
File file = new File(uploadDirectory,file1);
//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((sContentType = readLine(Linebyte,ai,servletinputstream,CharacterEncoding)) != null)
{
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}
fileoutputstream.close();
}
return filename;
}
private String readLine(byte Linebyte[],int ai[],ServletInputStream servletinputstream,String CharacterEncoding)
{
try
{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException _ex){
return null;
}
try{
if(CharacterEncoding == null){
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}
else{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}
catch(Exception _ex){
return null;
}
}
}
Top
2 楼xycleo()虚竹和尚()回复于 2002-06-19 09:12:08 得分 0
request.getParameter("file");Top
3 楼centerlife()回复于 2002-06-19 09:42:20 得分 0
通过另外一种途径解决了这个问题啦!
谢谢各位。
我原来只是想如何判断该控件为空值,可以通过字符串的长度或者判断“."的位置来确定。
现在有一个问题就是如何通过该控件来获取文件名,不要带路径!Top
4 楼weidegong(weidegong)回复于 2002-06-19 09:47:24 得分 10
<input type=file name=show>
<input type=button value=get onclick=get()>
<script>
function get(){
var str=document.all("show").value;
var arr=str.split('\\');
alert(arr[arr.length-1]);
}
</script>Top
5 楼centerlife()回复于 2002-06-19 09:53:13 得分 0
不用javascript,只用java,如何转换啊?我就是找不到相关的函数!Top
6 楼weidegong(weidegong)回复于 2002-06-19 10:04:21 得分 0
你用什么上传的?如果用smartUpload很容易得到文件名啊?
import java.util.regex.*;
String str="A|B|C|D";
Pattern patten=Pattern.compile("|");
String[] result = patten.split(str);Top
7 楼centerlife()回复于 2002-06-19 10:42:56 得分 0
已经结贴啦!但当时提交时出现错误。分已经给了。Top




