梦开始的地方reesun.blog.chinaunix.net
reesun
全部博文(333)
spider(0)
理论(0)
cpp(2)
Java(14)
PHP(12)
汇编(4)
python(12)
C语言(30)
hadoop(0)
sips(1)
Gluster(8)
Oracle(2)
nginx(4)
mysql(3)
Apache(2)
memcached(7)
Apache模块(5)
Apache管理(9)
服务器搭建(1)
Kernel Driver(3)
类Unix环境/系统(2)
redhat(4)
Ubuntu(38)
认证(0)
应用(8)
基础知识(32)
redis(3)
Postfix util源码(0)
memcached源代码(1)
magent源代码阅读(3)
超文本传送协议学(3)
JOS(10)
程序员(17)
摄影(3)
狗狗(0)
历史(1)
sports(2)
读书(3)
手表(0)
汽车(1)
随笔(3)
影评(0)
人生就是这样(8)
2015年(1)
2011年(116)
2010年(187)
2009年(25)
2008年(3)
2007年(1)
叶绍琛
wangchen
zcyyezi
pqwfree
小雅贝贝
ranson_z
cainoma
枫叶无霜
prolj
wjydlut
LikeFace
少林功夫
cynthia
Bsolar
Phyllis6
浪花小雨
sdhh_zha
13405800
分类: Java
2010-11-08 16:54:13
<%@ 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 = "<存储图片的目标路径+文件名>";
<%@ 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"); } %>
<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>
上一篇:Linux/Unix 新手和专家教程
下一篇:JSP注释、声明、指令、scriptlet、表达式
登录 注册