Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1881050
  • 博文数量: 333
  • 博客积分: 10791
  • 博客等级: 上将
  • 技术积分: 4314
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 07:39
文章分类

全部博文(333)

文章存档

2015年(1)

2011年(116)

2010年(187)

2009年(25)

2008年(3)

2007年(1)

分类: Java

2010-11-08 16:54:13

upload.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.awt.Image"%>
<%@ page import="java.awt.Image.*"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
String uploadPath = "<存储图片的目标路径>";
String realFilePath = 
"<存储图片的目标路径+文件名>";

boolean isMultipart = ServletFileUpload.isMultipartContent(request);


if(isMultipart==true){
    try{
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> items = upload.parseRequest(request);
        Iterator<FileItem> itr = items.iterator();
        while(itr.hasNext()){
            FileItem item=(FileItem)itr.next();
            String fileName=item.getName();
            if(fileName!=null){
                out.print(realFilePath);
                out.print("
"
);
                
                // to upload file        
                File uploadFile = new File(item.getName());
                File realFile = new File(uploadPath, realFileName);
                File savedFile = new File(uploadPath,uploadFile.getName());
                item.write(savedFile);
                savedFile.renameTo(realFile);
                
                //to generate a thumbnail                
                java.io.File sourceFile = new java.io.File(realFilePath);
                String miniPath = uploadPath1 + "/mini/" + uploadPath2 + "/" + realFileName;
                Image miniPicSrc = javax.imageio.ImageIO.read(sourceFile);   
                int new_w=60;
                int new_h=80;


                BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(miniPicSrc,0,0,new_w,new_h,null);
                FileOutputStream newimage = new FileOutputStream(miniPath);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
                encoder.encode(tag);
                newimage.close();
            }
        }
        out.print("upload succeed");
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
else{
    out.println("the enctype must be multipart/form-data");
}
%>



upload.htm

<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
        <title>File upload</title>
</head>
<body>
        <form name="myform" action="handleImage.jsp" method="post"
                enctype="multipart/form-data">
                File1:<br>
                <input type="file" name="myfile"><br>
                <input type="submit" name="submit" value="Commit">
        </form>
</body>
</html>


阅读(1339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~