分享
 
 
 

File upload by JSP

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

In forum, a lot of people are looking for another way to upload file by jsp,other than SmartUpload,which is a free bean from jspsmart.com.

Although its functionality is powerful, at least called "enough", the sourcecode is invisible to us,which is a little pity of this bean.

A few days ago,I got an article talking about the upload things on onjava.com.

and the author provided a piece of code to implement upload feature.

Click http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html to enjoy it!

However, there is also a flaw in his code, that's it only supports text plain file but binary format.In fact, most of files to upload are in binary format,aren't they?

But don't worry, I have made some modification on the original sourcecode to enable it to support all file formats.

Now I paste the whole source code below,

hopefully it can be helpful to those who are interested in this problem.

BTW: My Email is truelwl@sina.com, welcome to send mail and discuss with me on any Java technical issues.

Code:

import javax.servlet.http.HttpServletRequest;

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];

}

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()

{

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有