Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Servlet/JSP/JSF/JavaFX Script
打印话题 寄给朋友 订阅主题 |
作者 | Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 [Re:itking] |
iqueen
发贴: 4 |
于 2004-09-27 17:46
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.write; } 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); } } Eclipse使用技巧 |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
2507 | 问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 | itking | 909 | 2004-09-06 11:09 |
2068 | Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 | itking | 3 | 2004-09-06 16:42 |
2020 | Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 | itking | 9 | 2004-09-07 08:57 |
2060 | Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 | xml1123 | 62 | 2004-09-20 14:23 |
2054 | Re:问:我通过vb(xmlhttp)提交过来的文件,jsp怎样接收?下面是asp的,那位高人改改 | iqueen | 2816 | 2004-09-27 17:46 |
已读帖子 新的帖子 被删除的帖子 |
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 |