Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 [Re:itking]
iqueen





发贴: 4
于 2004-09-27 17:46 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FileUpload extends HttpServlet {

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.print("File upload success. <a href=\"/jspbook/files/");

out.print("\">Click here to browse through all uploaded ");

out.println("files.</a><br>");

ServletInputStream sis = request.getInputStream();

StringWriter sw = new StringWriter();

int i = sis.read();

for (;i!=-1&&i!='\r';i=sis.read()) {

sw.writeLight Bulb;

}

sis.read(); // ditch '\n'

String delimiter = sw.toString();

int count = 0;

while(true) {

StringWriter h = new StringWriter();

int[] temp = new int[4];

temp[0] = (byte)sis.read();

temp[1] = (byte)sis.read();

temp[2] = (byte)sis.read();

h.write(temp[0]);

h.write(temp[1]);

h.write(temp[2]);

// read header

for (temp[3]=sis.read();temp[3]!=-1;temp[3]=sis.read()) {

if (temp[0] == '\r' &&

temp[1] == '\n' &&

temp[2] == '\r' &&

temp[3] == '\n') {

break;

}

h.write(temp[3]);

temp[0] = temp[1];

temp[1] = temp[2];

temp[2] = temp[3];

}

String header = h.toString();

int startName = header.indexOf("name=\"");

int endName = header.indexOf("\"",startName+6);

if (startName == -1 || endName == -1) {

break;

}

String name = header.substring(startName+6, endName);

if (name.equals("file")) {

startName = header.indexOf("filename=\"");

endName = header.indexOf("\"",startName+10);

String filename =

header.substring(startName+10,endName);

ServletContext sc =

request.getSession().getServletContext();

File file = new File(sc.getRealPath("/files"));

file.mkdirs();

FileOutputStream fos =

new FileOutputStream(

sc.getRealPath("/files")+"/"+filename);

// write whole file to disk

int length = 0;

delimiter = "\r\n"+delimiter;

byte[] body = new byte[delimiter.length()];

for (int j=0;j<body.length;j++) {

body[j] = (byte)sis.read();

}

// check it wasn't a 0 length file

if (!delimiter.equals(new String(body))) {

int e = body.length-1;

i=sis.read();

for (;i!=-1;i=sis.read()) {

fos.write(body[0]);

for (int l=0;l<body.length-1;l++) {

body[l]=body[l+1];

}

body[e] = (byte)i;

if (delimiter.equals(new String(body)))

break;

}

length++;

}

}

fos.flush();

fos.close();

}

if (sis.read() == '-' && sis.read() == '-')

break;

}

}

out.println("</html>");

}

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

doPost(request, response);

}

}




话题树型展开
人气 标题 作者 字数 发贴时间
2459 问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 itking 909 2004-09-06 11:09
2023 Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 itking 3 2004-09-06 16:42
1974 Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 itking 9 2004-09-07 08:57
2013 Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 xml1123 62 2004-09-20 14:23
2002 Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 iqueen 2816 2004-09-27 17:46

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923