我想学学文件上传,现在是用servlet做的,但是解析输入流的时候总是不对,我的页面上除了file外还有几个text,select我同时也想得到他们的值,我不知道有uploadfile包和jspsmartupload包可不可以。于是我直接解析的到的流,但是解析的时候总是出错,所以来这里请高手帮助,希望前辈们给个例子,最后是我用的那种方式,谢谢

解决方案 »

  1.   

    smartupload就行啊
    网上很多例子的
      

  2.   

    jspsmart网上资料很多,都是下完直接就能用的
      

  3.   

    我自己写了一个,但注意,在有些系统上可能不支持中文路径:
    如果你想再传其它参数,应该把参数手动拼到URL后面如:form1.action=form1.action+"?arg1="+form1.name.value;
    upload.jsp代码:
    <%@ page language="java"
        pageEncoding="GBK"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    <form name="form1" action="/upload/UploadServlet" method="POST" ENCTYPE="multipart/form-data">
    <input type="file" name="file1"/><br>
    复核人:<input name="verify" value="12312312312"/><input name="verify1" value="12312312312"/><br>
    <input type="submit"value="上传"/>
    </form>
    </body>
    </html>
    UploadServlet 代码:
    package servlet;import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import save.SaveFile;public class UploadServlet extends HttpServlet {
       private static final String LINE_SEPARATOR ="\r\n"; /**
     * Constructor of the object.
     */
    public UploadServlet() {
    super();
    } /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doPost(request,response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
     String contentType = request.getContentType();
    InputStream in1=request.getInputStream();
    int offset = -1;
    StringBuffer sb=new StringBuffer();
    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\abc.zip"));
    byte[] b=new byte[4096];
    while((offset=in1.read(b,0,4096))>-1){
    sb.append(new String(b,0,offset,"UTF-8"));
    bos.write(b, 0, offset);
    }
    bos.flush();
    bos.close(); }
    public void init() throws ServletException {
    // Put your code here
    }}
      

  4.   

    对了,我这个是写死向D盘存一个ZIP文件,那么我上传的也是一个ZIP文件,你可以自行修改,