还是SmartUpload,嘿嘿………:)你为什么要找另一个呢?有什么问题?而且你拭过自己写一个吗?网上也有这方面的资料,一个很流行的上传文件的bean代码,你手上有吗?
解决方案 »
- java 中输入输出中类的构造方法不太理解,求解,感激不尽!
- 关于PriorityQueue不得不说的事
- 阅读别人的源代码有没有技巧可寻?
- 问个JAVA问题,冒似有点难!
- 怎么设置一个类似全局变量作用的变量?在线等
- Thead 中要等待一件事情完成,有什么好的方法? 要时时检测
- 弱弱的问一个新手的问题
- 《Effective Java》侯捷译下载
- hashCode为什么这么写?
- 又是一个巨简单的问题,怎么偶老是问这样的问题?
- 以下代码在Jcreator pro里编译为何有错?98里面!
- 我安装好TOMCAT,我也配置好了path,java_home,tomcat_home,用startup.bat也运行了,可是tomcat首页出不来,为什么?我的电脑是w2k.
哦,这样就没办法了。你一直是用apusic吗?这个好用吗,能否介绍一下。不是他们自己介绍的广告语,是你使用的心得。多谢.
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;public class FileUploadBean{
private String savePath, filepath, filename, contentType;
private Dictionary fields;
//Begin Rayman's modification
private static final int BUF_SIZE = 256;
//End Rayman's modification public String getFilename() {
return filename;
} public String getFilepath() {
return filepath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
} public String getContentType() {
return contentType;
} public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null)
{
//System.out.println(fieldName);
//System.out.println("if loop");
return null;
}
//System.out.println(fieldName);
//System.out.println(fields.get("company"));
return (String)fields.get(fieldName);
} private void setFilename(String s) { if (s==null)
return; int pos = s.indexOf("filename=\"");
if (pos != -1) {
filepath = s.substring(pos+10, s.length()-1);
// Windows browsers include the full path on the client
// But Linux/Unix and Mac browsers only send the filename
// test if this is from a Windows browser
pos = filepath.lastIndexOf("\\");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
} private void setContentType(String s){
if (s==null)
return; int pos = s.indexOf(": ");
if (pos != -1)
contentType = s.substring(pos+2, s.length());
} public void doUpload(HttpServletRequest request) throws IOException{ ServletInputStream in = request.getInputStream();
byte[] line = new byte[BUF_SIZE];
int i = in.readLine(line, 0, 128);
if (i < 3)
return;
int boundaryLength = i - 2; String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
fields = new Hashtable(); while (i != -1) {
String newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
if (newLine.indexOf("filename=\"") != -1) {
setFilename(new String(line, 0, i-2));
if (filename==null)
return;
//this is the file content
i = in.readLine(line, 0, 128);
setContentType(new String(line, 0, i-2));
i = in.readLine(line, 0, 128);
//Begin Rayman's modification
FileOutputStream pw = new FileOutputStream((savePath==null? "" : savePath) + filename);
boolean done = false;
int k = 0;
byte c1 = 0x0d;
byte c2 = 0x0a; byte [] boundarybytes = boundary.getBytes();
byte [] magickey = new byte[boundarybytes.length + 2];
magickey[0] = c1;
magickey[1] = c2;
for(int t = 0;t<boundarybytes.length;t++)
{
magickey[2+t] = boundarybytes[t];
}
for(int j = 0;j<magickey.length;j++)
{
System.out.print((int)magickey[j]);
}
System.out.println("magickey==" + new String(magickey));
System.out.println("magickey's length ==" + magickey.length);
byte [] buf = new byte[magickey.length];
int count = 0;
byte c = -1;
boolean pause = false;
while (true) {
// the problem is the last line of the file content
// contains the new line character.
// So, we need to check if the current line is
// the last line
if(!pause)
{
byte [] one = new byte[1];
if(in.read(one)==-1) break;
c = one[0];
count ++;
}
else
{
pause = false;
} if(c==magickey[k])
{
System.out.print((int)c + " " + (char)c + " ");
if(k == magickey.length-1)
{
//It's last line.
System.out.println("Last line!" + " count==" + count);
break;
}
buf[k] = (byte)c;
k++;
}
else
{
if(k>0)
{ pw.write(buf,0,k); }
if(c==magickey[0])
{
pause = true;
}
else
{
pw.write(c);
}
k=0;
}
}
pw.close();
//End Rayman's modification }
else {
//this is a field
// get the field name
int pos = newLine.indexOf("name=\"");
String fieldName = newLine.substring(pos+6, newLine.length()-3);
//System.out.println("fieldName:" + fieldName);
// blank line
i = in.readLine(line, 0, 128);
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
StringBuffer fieldValue = new StringBuffer(128);
while (i != -1 && !newLine.startsWith(boundary)) {
// The last line of the field
// contains the new line character.
// So, we need to check if the current line is
// the last line.
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary)))
fieldValue.append(newLine.substring(0, newLine.length()-2));
else
fieldValue.append(newLine);
newLine = new String(line, 0, i);
}
//System.out.println("fieldValue:" + fieldValue.toString());
fields.put(fieldName, fieldValue.toString());
}
}
i = in.readLine(line, 0, 128); } // end while
} public FileUploadBean()
{
}
public static void main(String [] args)
{
/* try
{
new FileUploadBean().raymanPseudoUpload();
}catch(Exception e)
{
e.printStackTrace();
}*/
}}
http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html
However ,it seems the foreign expert's code doesn't work when a non-plain text file is uploaded,
so I made a small modification on it.
Now it can support both plain text and binary file,
hopefully it 's helpful.
上面的也不错的
is good thing!!!
Orangehk.com is using too!!!