日期:2014-05-16 浏览次数:20392 次
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.jspsmart.upload.*"%> <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>使用smartUpload上传文件</title> </head> <body> <% try { //System.out.println(request.getInputStream()); //System.out.println(mySmartUpload.getRequest().getParameter("bookName"));//都还没用组件解析怎么可能会有值 mySmartUpload.initialize(pageContext); mySmartUpload.upload();//解析完毕 System.out.println(mySmartUpload.getRequest().getParameter("bookName"));//可以取到一起提交的表单的附加值了 mySmartUpload.save("F:/upload/");//项目根目录下的upload文件夹下 out.println("文件上传成功!"); } catch (Exception e) { e.printStackTrace(); } %> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>上传图片</title> </head> <body> <form action="<%=request.getContextPath()%>/upload" method="post" enctype="multipart/form-data"> <input type="text" name="haha" value="haha"/> <input type="file" name="image" /> <input type="submit" value="提交" /></form> </body> </html>
package org.dean.servlet; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; import java.util.Hashtable; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class UploadFileServlet */ public class UploadFileServlet extends HttpServlet { private static final long serialVersionUID = -6087978448595987514L; public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { final int NONE = 0; // 状态码,表示没有特殊操作 final int DATAHEADER = 1; // 表示下一行要读到报头信息 final int FILEDATA = 2; // 表示下面要读的是上传文件和二进制数据 final int FIELDDATA = 3; // 表示下面要读到表单域的文本值 // 请求消息实体的总长度(请求消息中除消息头之外的数据长度) int totalbytes = req.getContentLength(); // 容纳请求消息实体的字节数组 byte[] b = new byte[totalbytes]; // 请求消息类型 String contentType = req.getContentType(); String fieldname = ""; // 表单域的名称 String fieldvalue = ""; // 表单域的值 String filename = ""; // 上传的文件名称 String boundary = ""; // 分界符字符串 String lastboundary = ""; // 结束分界符字符串 int fileSize = 0; // 文件长度 // 容纳表单域的名称/值的哈希表 Hashtable formfields = new Hashtable(); // 在消息头类型中找到分界符的定义 int pos = contentType.indexOf("boundary="); String fileID; // 上传的文件ID if (pos != -1) { pos += "boundary=".length(); boundary = "--" + contentType.substring(pos); // 解析出分界符 lastboundary = boundary + "--"; // 得到结束分界符 } int state = NONE; // 起始状态为NONE // 得到请求消息的数据输入流 DataInputStream in = new DataInputStream(req.getInputStream()); in.readFully(b); // 根据长度,将消息实体的内容读入字节数组b中 in.close(); // 关闭数据流 String reqconte