当前位置: 技术问答>java相关
如何获取file控件的值!(在线等待)
来源: 互联网 发布时间:2015-06-25
本文导语: 使用file控件时,但是没有点取该控件时,此时该控件的值等于什么呢? | import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletInputStream; import javax.servlet.Servl...
使用file控件时,但是没有点取该控件时,此时该控件的值等于什么呢?
|
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 = s.length() - 1)
{
i = s.lastIndexOf("/");
if(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;
}
}
}
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 = s.length() - 1)
{
i = s.lastIndexOf("/");
if(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;
}
}
}
|
function get(){
var str=document.all("show").value;
var arr=str.split('\');
alert(arr[arr.length-1]);
}