服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jsp文件上传体会(采用javabean上传)

jsp文件上传体会(采用javabean上传)

 

从网上当了一个例子,发觉上传图片文件时图片文件中内容发生了变化,最大的变化时所有的ff变成了3f

也就是说输出时把字符当成有符号传送了?(是这样把?)

使用代码如下:

printwriter pw = new printwriter(new bufferedwriter(new

filewriter((savepath==null? "" : savepath) + filename)));

while (i != -1 && !newline.startswith(boundary)) {

// 文件内容的最后一行包含换行字符

// 因此我们必须检查当前行是否是最

// 后一行

i = in.readline(line, 0, 1280);

if ((i==boundarylength+2 || i==boundarylength+4)

&& (new string(line, 0, i).startswith(boundary)))

pw.print(newline.substring(0, newline.length()-2));

else

pw.print(newline);

newline = new string(line, 0, i);

 

}

pw.close();

 

但是总是不行,后来我把printwriter用fileoutputstream替代,代码如下:

fileoutputstream pw=new fileoutputstream((savepath==null? "" : savepath) + filename);

//printwriter pw = new printwriter(new bufferedwriter(new

//filewriter((savepath==null? "" : savepath) + filename)));

while (i != -1 && !newline.startswith(boundary)) {

// 文件内容的最后一行包含换行字符

// 因此我们必须检查当前行是否是最

// 后一行

i = in.readline(line, 0, 1280);

if ((i==boundarylength+2 || i==boundarylength+4)

&& (new string(line, 0, i).startswith(boundary)))

pw.write(newline.substring(0, newline.length()-2).getbytes("iso8859_1"));

else

pw.write(newline.getbytes("iso8859_1"));

newline = new string(line, 0, i,"iso8859_1");

 

}

pw.close();

竟然就可以了,那个转换的编码还一定要上才行,不然也不干活的,真是郁闷,这跨平台就不能让写code的人解放解放,一个输出都n多类,看着心寒,想着简直想死啊

忙了我一个下午,原来就是这回事

 

下面把原代码附上,是把别人的代码改吧改吧放上来的,还请原作者不要见意:

java bean 文件:

package cr.web;

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.*;

public class fileuploadbean {

 

private string savepath, filepath, filename, contenttype;

private byte[] b;

byte t;

private dictionary fields;

 

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)

return null;

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浏览器发送完整的文件路径和名字

// 但linux/unix和mac浏览器只发送文件名字

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 getbyte(httpservletrequest request)

{

datainputstream is;

 

int i=0;

try

{

is=new datainputstream(request.getinputstream());

b=new byte[request.getcontentlength()];

 

while (true)

{

try

{

t=is.readbyte();

b[i]=t;

i++;

}

catch(eofexception e)

{ break;}

}

is.close();}

catch(ioexception e)

{}

}

 

public void doupload1(httpservletrequest request) throws

ioexception {

byte[] line=new byte[128];

fileoutputstream os=new fileoutputstream("c://demo.out");

servletinputstream in = request.getinputstream();

getbyte(request);

string temp="";

temp=new string(b,"iso8859_1");

byte[] img=temp.getbytes("iso8859_1");

for (int i=0;i<img.length;i++)

{ os.write(img[i]); }

os.close();

}

 

 

 

public void doupload(httpservletrequest request) throws ioexception {

request.setcharacterencoding("gb2312");

servletinputstream in = request.getinputstream();

 

byte[] line = new byte[1280];

int i = in.readline(line, 0, 1280);

if (i < 3)

return;

int boundarylength = i - 2;

 

string boundary = new string(line, 0, boundarylength); //-2丢弃换行字符

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;

//文件内容

i = in.readline(line, 0, 1280);

setcontenttype(new string(line, 0, i-2));

i = in.readline(line, 0, 1280);

//空行

i = in.readline(line, 0, 1280);

newline = new string(line, 0, i,"iso8859_1");

fileoutputstream pw=new fileoutputstream((savepath==null? "" : savepath) + filename);

//printwriter pw = new printwriter(new bufferedwriter(new

//filewriter((savepath==null? "" : savepath) + filename)));

while (i != -1 && !newline.startswith(boundary)) {

// 文件内容的最后一行包含换行字符

// 因此我们必须检查当前行是否是最

// 后一行

i = in.readline(line, 0, 1280);

if ((i==boundarylength+2 || i==boundarylength+4)

&& (new string(line, 0, i).startswith(boundary)))

pw.write(newline.substring(0, newline.length()-2).getbytes("iso8859_1"));

else

pw.write(newline.getbytes("iso8859_1"));

newline = new string(line, 0, i,"iso8859_1");

 

}

pw.close();

 

}

else {

// 普通表单输入元素

// 获取输入元素名字

int pos = newline.indexof("name=/"");

string fieldname = newline.substring(pos+6, newline.length()-3);

 

i = in.readline(line, 0, 1280);

i = in.readline(line, 0, 1280);

newline = new string(line, 0, i);

stringbuffer fieldvalue = new stringbuffer(1280);

while (i != -1 && !newline.startswith(boundary)) {

// 最后一行包含换行字符

// 因此我们必须检查当前行是否是最后一行

i = in.readline(line, 0, 1280);

if ((i==boundarylength+2 || i==boundarylength+4)

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

}

fields.put(fieldname, fieldvalue.tostring());

}

}

i = in.readline(line, 0, 1280);

 

}

}

}

 

html页面:

<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312">

<title>文件上载</title>

</head>

 

<body>

<form action=jsp1.jsp enctype="multipart/form-data" method=post>

作者: <input type=text name=author>

<br>

公司: <input type=text name=company>

<br>

说明: <textarea name=comment></textarea>

<br>

选择要上载的文件<input type=file name=filename>

<br>

文件描述: <input type=text name=description>

<br>

<input type=submit value="upload">

</form>

</body>

</html>

 

jsp页面:

<%@ page contenttype="text/html;charset=gb2312"%>

<jsp:usebean id="thebean" scope="page" class="cr.web.fileuploadbean" />

<%

thebean.setsavepath("d://");

 

//thebean.doupload1(request);

thebean.doupload(request);

out.println("filename:" + thebean.getfilename());

out.println("<br>内容类型:" + thebean.getcontenttype());

out.println("<br>作者:" + thebean.getfieldvalue("author"));

out.println("<br>公司:" + thebean.getfieldvalue("company"));

out.println("<br>说明:" + thebean.getfieldvalue("comment"));

%>

 

 

 

扫描关注微信公众号