Topic: 请问:谁有JSP图片上传源码??

  Print this page

1.请问:谁有JSP图片上传源码?? Copy to clipboard
Posted by: 酒惠姊
Posted on: 2006-05-07 13:32

谁有JSP图片上传源码,借小妹一看,在此谢过!!

2.Re:谁有JSP图片上传源码?? [Re: 酒惠姊] Copy to clipboard
Posted by: voit25
Posted on: 2006-05-10 16:25

我也要啊!

哈哈,分享一下!

3.Re:谁有JSP图片上传源码?? [Re: 酒惠姊] Copy to clipboard
Posted by: 酒惠姊
Posted on: 2006-05-19 23:06

现在好想知道呀,拜托各位高手了!!!

4.Re:请问:谁有JSP图片上传源码?? [Re: 酒惠姊] Copy to clipboard
Posted by: majianxiong
Posted on: 2006-05-23 18:04


5.Re:请问:谁有JSP图片上传源码?? [Re: 酒惠姊] Copy to clipboard
Posted by: majianxiong
Posted on: 2006-05-23 18:04


6.Re:请问:谁有JSP图片上传源码?? [Re: majianxiong] Copy to clipboard
Posted by: binge
Posted on: 2006-05-23 19:20

You can use apache-commons-fileupload with apache-commons-io component to upload files
If you not have any example to use it
can find it on the official site http://jakarta.apache.org/commons/fileupload/using.html
or use my implement class from apache example

see hereinafter
you must to change some code for normal use in your projects


/**
* $Id: Upload.java , 2006-5-21 20:10:58 WilliamRaym Exp $
* Copyright By WilliamRaym 2006-2006
*/

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.LogFactory;

import com.sun.mail.iap.Response;
import com.williamraym.properties.Property;

public class Upload extends HttpServlet {

  private static final int SIZEMAX = 10 * 1024 * 1024;// 10MB

  private static final int SIZETHRESHOLD = 1024;// 1MB

  public Upload() {
  }

  private org.apache.commons.logging.Log log = LogFactory.getLog(this.getClass());

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html; charset=UTF-8");
    response.setHeader("cache","none");
    PrintWriter out = response.getWriter();
    out
        .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out
        .println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />");
    out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println(" <BODY>");
    request.setCharacterEncoding("UTF-8");
    boolean writeToFile = true;
    DiskFileItemFactory factory = new DiskFileItemFactory();
    String tempFilePath = new String(Property
        .getProperty("williamraym.home.dir")
        + Property.getFileSeparator() + "temp");
    com.williamraym.file.File.createDirectory(tempFilePath);
    factory.setRepository(new File(tempFilePath));
    factory.setSizeThreshold(SIZETHRESHOLD);
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(SIZEMAX);
    log.debug(SIZEMAX);
    upload.setHeaderEncoding("UTF-8");
    try {
      List items = upload.parseRequest(request);
      Iterator iter = items.iterator();
      while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
          out.println("1" + item.getFieldName());
          out.println("4"
              + new String(item.getString()
                  .getBytes("ISO-8859-1"), "UTF-8"));
          log.info(item.getFieldName());
          log.info(new String(
              item.getString().getBytes("ISO-8859-1"), "UTF-8"));
        } else {
          String n = item.getName();
          String filename = n.substring(n.lastIndexOf("\\") + 1, n
              .length());
          String uploadFilePath = Property
              .getProperty("williamraym.home.dir")
              + Property.getFileSeparator()
              + "upload"
              + Property.getFileSeparator();
          com.williamraym.file.File.createDirectory(uploadFilePath);
          String uploadFile = uploadFilePath + filename;
          log.info(uploadFilePath);
          if (writeToFile) {
            File uploadedFile = new File(uploadFile);
            try {
              item.write(uploadedFile);
            } catch (Exception e) {
              log.info(e.getMessage());
              e.printStackTrace();
            }
          } else {
            InputStream uploadedStream = item.getInputStream();
            uploadedStream.close();
          }
        }
        item.delete();
      }
    } catch (FileUploadException fue) {
      fue.getMessage();
      log.info(fue.getMessage());
    }

    out.println(" </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
  }
}



   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