3000分求JSP图片上传/放大缩小/裁减的源代码

sdyqingdao 2009-08-27 10:33:47
3000分求JSP图片上传/放大缩小/裁减的源代码。能裁减固定大小比如(120*100)的就行。有预览,能显示图片长宽和大小(file size)最好。

本人结帖率是99.87%, 可用分8000+

非常感谢!!
...全文
3417 101 打赏 收藏 举报
写回复
101 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
weiyaku 2011-12-28
wait?
  • 打赏
  • 举报
回复
wilber 2011-11-14
肿么还没得结果么?
  • 打赏
  • 举报
回复
veronica3057 2011-08-26
顶一个,受教了,看能模拟得出不
  • 打赏
  • 举报
回复
xjdwto 2011-08-01
谢谢,正要用到这方面的知识
  • 打赏
  • 举报
回复
liufeiBest 2010-01-19
什么东西?高不明白!
  • 打赏
  • 举报
回复
z050301402 2010-01-16
来看下
  • 打赏
  • 举报
回复
amdgaming 2009-10-18
jf
  • 打赏
  • 举报
回复
yang677888 2009-10-07
看看 学习啦
  • 打赏
  • 举报
回复
dahaidao 2009-10-07
没有办法试验了.
  • 打赏
  • 举报
回复
study_ID01 2009-10-07
直接加Q 发案例 多方便啊 代码 一溜打 怎么看得明白!
  • 打赏
  • 举报
回复
pean19 2009-09-28
路过,看看结果
  • 打赏
  • 举报
回复
xutao_2008_2000 2009-09-28
重赏之下必有勇夫,看来并非是扯淡呀!
  • 打赏
  • 举报
回复
wanglingzhong 2009-09-28
关注下~~~~
  • 打赏
  • 举报
回复
KelvenCheung 2009-09-28
期待中...顺便接点分...
  • 打赏
  • 举报
回复
长公子冰 2009-09-23
哇塞 我就来看看热闹
  • 打赏
  • 举报
回复
yxtyxt3311 2009-09-23
<html>
<head>
<title> </title>
<link href="${pageContext.request.contextPath}/css/main.css" type="text/css" rel="Stylesheet" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/default.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/JS/jquery1.2.6.pack.js"> </script>
<script type="text/javascript" src="${pageContext.request.contextPath}/JS/ui.core.packed.js" > </script>
<script type="text/javascript" src="${pageContext.request.contextPath}/JS/ui.draggable.packed.js" > </script>
<script type="text/javascript" src="${pageContext.request.contextPath}/JS/CutPic.js"> </script>
<script type="text/javascript">
function Step1() {
$("#Step2Container").hide();
}

function Step2() {
$("#CurruntPicContainer").hide();
}
function Step3() {
$("#Step2Container").hide();
}


</script>
</head>
<body>

<div id="outer">

<div id="upbg"> </div>

<div id="inner">

<div id="header">
<h1> <span>EduAsis </span> <sup>1.0 </sup> </h1>
<h2>by heyee </h2>
</div>

<div id="splash"> </div>

<div id="menu">
<h3>用户注册第二步:头像上传 </h3>

<div id="date"> <b> <%SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd a hh:mm:ss");%>
<%=format.format(new Date())%>
</b> </div>
</div>


<div id="primarycontent">

<!-- primary content start -->

<div class="post" style="width:650px">
<div class="header">
<h3>头像上传 </h3>
</div>
<div class="content" style="width:650px">
<div>
<div class="left">
<!--当前照片-->
<div id="CurruntPicContainer">
<div class="title"> <b>当前照片 </b> </div>
<div class="photocontainer">
<img id="imgphoto" src=" <%=defaultPic%>" style="border-width:0px;width:120;height:120" />
</div>
</div>
<!--Step 2-->
<div id="Step2Container">
<div class="title"> <b> 裁切头像照片 </b> </div>
<div class="uploadtooltip">您可以拖动照片以裁剪满意的头像 </div>
<div id="Canvas" class="uploaddiv">

<div id="ImageDragContainer">
<img id="ImageDrag" class="imagePhoto" src="/EduAsis/student/${picurl}" style="border-width:0px;" />
</div>
<div id="IconContainer">
<img id="ImageIcon" class="imagePhoto" src="/EduAsis/student/${picurl}" style="border-width:0px;" />
</div>
</div>
<div class="uploaddiv">
<table>
<tr>
<td id="Min">
<img alt="缩小" src="/EduAsis/image/_c.gif" onmouseover="this.src='image/_c.gif';" onmouseout="this.src='image/_h.gif';" id="moresmall" class="smallbig" />
</td>
<td>
<div id="bar">
<div class="child">
</div>
</div>
</td>
<td id="Max">
<img alt="放大" src="/EduAsis/image/c.gif" onmouseover="this.src='/EduAsis/image/c.gif';" onmouseout="this.src='/EduAsis/image/h.gif';" id="morebig" class="smallbig" />
</td>
</tr>
</table>
</div>
<form action="saveIcon.do" method="post">
<div class="uploaddiv">
<input type="submit" name="btn_Image" value="保存头像" id="btn_Image" />
</div>
<div>
<!-- 文件名称 --> <input name="filename" type="hidden" value="${picurl}" id="txt_filename" /> <br />
<!-- 距离顶部 --> <input name="txt_top" type="hidden" value="82" id="txt_top" /> <br />
<!-- 距离左边 --> <input name="txt_left" type="hidden" value="73" id="txt_left" /> <br />
<!-- 截取框的宽--> <input name="txt_DropWidth" type="hidden" value="120" id="txt_DropWidth" /> <br />
<!-- 截取框的高--> <input name="txt_DropHeight" type="hidden" value="120" id="txt_DropHeight" /> <br />
<!-- 放大倍数 --> <input name="txt_Zoom" type="hidden" id="txt_Zoom" />
</div> </form>
</div>

</div>
<form name="form1" method="post" action="uploadPic.do" id="form1" enctype="multipart/form-data">
<div class="right">
<!--Step 1-->
<div id="Step1Container">
<div class="title"> <b>更换照片 </b> </div>
<div id="uploadcontainer">
<div class="uploadtooltip">请选择新的照片文件,文件需小于2.5MB </div>
<div class="uploaddiv"> <input type="file" name="pic" id="fuPhoto" title="选择照片" /> </div>
<div class="uploaddiv">
<input type="submit" name="btnUpload" value="上 传" id="btnUpload" />
<input type="button" name="finish" value="完 成" id="finish" onclick="window.location='main.do';" />
</div>

</div>

</div>
</div>
</form>
</div>
<%
if(null==picUrl||"".equals(picUrl))
{%>
<script type='text/javascript'>Step1(); </script>
<%}else if(!"".equals(picUrl)&& "2".equals(step)){
%>
<script type='text/javascript'>Step2(); </script>
<%}else if(!"".equals(picUrl)&& "3".equals(step)){
%>
<script type='text/javascript'>Step3(); </script>
<%}%>
</div>
</div>
<!-- primary content end -->
</div>
<div id="footer">
© GorTon. All rights reserved. Design by GorTon.
</div>
</div>
</div>
</body>
</html>
  • 打赏
  • 举报
回复
yxtyxt3311 2009-09-23
Java代码
1.action

package edu.sxau.eduasis.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.upload.FormFile;

import edu.sxau.eduasis.domain.CollegeInfo;
import edu.sxau.eduasis.domain.Student;
import edu.sxau.eduasis.form.StudentForm;
import edu.sxau.eduasis.form.UploadPicForm;
import edu.sxau.eduasis.service.CollegeInfoService;
import edu.sxau.eduasis.service.StudentService;
import edu.sxau.eduasis.utils.ImageCut;

public class RegisterAction extends MappingDispatchAction{
private StudentService ss; //--
private CollegeInfoService cs; //--
public void setCs(CollegeInfoService cs) {
this.cs = cs;
}

public void setSs(StudentService ss) {
this.ss = ss;
}
//上传照片
public ActionForward uploadPic(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
Student student = (Student)session.getAttribute("user");
UploadPicForm f = (UploadPicForm)form;
FormFile ff = f.getPic(); //--
String fileName=ff.getFileName();

//-- 如果浏览框中的文件名为空,则返回本页面
if(fileName.trim().equals("")){
return mapping.findForward("fail");
}



fileName=student.getLoginName()+"."+fileName.split("\\.")[1];
String path="student\\"+student.getLoginName();
path=this.getServlet().getServletContext().getRealPath(path);
File pathFile = new File(path);
if(!pathFile.exists()){
pathFile.mkdirs();
}
File file = new File(path,fileName);
BufferedInputStream bis=
new BufferedInputStream(ff.getInputStream());
BufferedOutputStream bos=
new BufferedOutputStream(new FileOutputStream(file));
int b=-1;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
request.setAttribute("step", 2);
request.setAttribute("picurl", "/"+student.getLoginName()+"/"+fileName);
return mapping.findForward("success");
}
//裁剪照片
public ActionForward saveIcon(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
Student student = (Student)session.getAttribute("user");
Double zoom = Double.valueOf(request.getParameter("txt_Zoom"));
double x = Double.valueOf(request.getParameter("txt_top"))/zoom;
double y = Double.valueOf(request.getParameter("txt_left"))/zoom;
double width = Double.valueOf(request.getParameter("txt_DropWidth"))/zoom;
double height = Double.valueOf(request.getParameter("txt_DropHeight"))/zoom;
String name = request.getParameter("filename");
x=x <0?0:x;
y=y <0?0:y;
ImageCut imageCut = new ImageCut((int)y,(int)x,(int)width,(int)height);
String path="student\\"+student.getLoginName();
path=this.getServlet().getServletContext().getRealPath(path);
name=name.split("/")[2];
imageCut.setSrcpath(path+"\\"+name);
imageCut.setSubpath(path+"\\"+name);
request.setAttribute("step", 3);
request.setAttribute("picurl","/"+student.getLoginName()+"/"+name);
imageCut.cut();
student.setIconPath("/EduAsis/student/"+student.getLoginName()+"/"+name);
ss.updateStudent(student);
return mapping.findForward("success");
}
//注册个人信息:
public ActionForward regtwo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("utf-8");
StudentForm sf = (StudentForm)form;
Student student = new Student();
BeanUtils.copyProperties(student,sf);
String collegeName = sf.getCollegeName();
String departmentName = sf.getDepartmentName();
System.out.println("departmentName"+departmentName);
CollegeInfo collegeInfo = cs.findCollege(collegeName,departmentName);
student.setCollegeInfo(collegeInfo);
ss.addStudent(student);
HttpSession session = request.getSession();
session.setAttribute("user", student);
return mapping.findForward("success");
}
}
Java代码
2 工具类

package edu.sxau.eduasis.utils;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageCut{
private String srcpath ;
private String subpath ;
private int x ;
private int y ;
private int width ;
private int height ;
public ImageCut(){
}
public ImageCut(int x,int y,int width,int height){
this.x = x ;
this.y = y ;
this.width = width ;
this.height = height ;
}
public void cut() throws IOException{
FileInputStream is = null ;
ImageInputStream iis =null ;
try{
is = new FileInputStream(srcpath);
Iterator <ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis,true) ;
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
ImageIO.write(bi, "jpg", new File(subpath));
}
finally{
if(is!=null)
is.close() ;
if(iis!=null)
iis.close();
}
}
public int getHeight(){
return height;
}

public void setHeight(int height){
this.height = height;
}

public String getSrcpath(){
return srcpath;
}

public void setSrcpath(String srcpath){
this.srcpath = srcpath;
}

public String getSubpath(){
return subpath;
}

public void setSubpath(String subpath){
this.subpath = subpath;
}

public int getWidth(){
return width;
}

public void setWidth(int width){
this.width = width;
}

public int getX(){
return x;
}

public void setX(int x){
this.x = x;
}

public int getY(){
return y;
}

public void setY(int y){
this.y = y;
}

public static void main(String[] args)throws Exception{
String name = "/home/soft01/03.jpg";
ImageCut o = new ImageCut(100,100,100,100);
o.setSrcpath(name);
o.setSubpath("/home/soft01/2.jpg");
o.cut() ;
}
}
Java代码
3.form
package edu.sxau.eduasis.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadPicForm extends ActionForm{

/**
*
*/
private static final long serialVersionUID = -7700785803356319532L;
private FormFile pic;
public FormFile getPic() {
return pic;
}
public void setPic(FormFile pic) {
this.pic = pic;
}
}
XML/HTML代码
4.上传照片的jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@page import="java.text.*,java.util.*" %>
<%
String picUrl = (String)request.getAttribute("picurl");
String step = ((Integer)request.getAttribute("step"))+"";
String defaultPic ="/EduAsis/images/man.GIF";
if("3".equals(step))
defaultPic = "/EduAsis/student/"+picUrl;
%>

  • 打赏
  • 举报
回复
yxtyxt3311 2009-09-23
import java.io.file;
import java.awt.image.bufferedimage;
import java.awt.image;
import java.awt.image.affinetransformop;
import javax.imageio.imageio;
import java.awt.geom.affinetransform;

public class uploadimg {

string fromdir; // fromdir 图片的原始目录
string todir; // todir 处理后的图片存放目录
string imgfile; // imgfile 原始图片
string sysimgfile; // sysimgfile 处理后的图片文件名前缀

uploadimg( string fromdirnow, string todirnow, string imgfilenow, string sysimgfilenow ){
fromdir = fromdirnow;
todir = todirnow;
imgfile = imgfilenow;
sysimgfile = sysimgfilenow;
}

public boolean createthumbnail() throws exception
{
// fileextnmae是图片的格式 gif jpg 或png
// string fileextnmae="";
double ratio = 0.0;
file f = new file(fromdir,imgfile);
if ( !f.isfile() )
throw new exception(f+" is not image file error in createthumbnail!");

//首先判断上传的图片是gif还是jpg imageio只能将gif转换为png
// if (isjpg(imgfile)){
// fileextnmae="jpg";
// }
//else{
// fileextnmae="png";
// }
file thf = new file( todir, sysimgfile + ".jpg" );

bufferedimage bi = imageio.read(f);
//假设图片宽 高 最大为120 120
image itemp = bi.getscaledinstance (120,120,bi.scale_smooth);

if ((bi.getheight()>120) || (bi.getwidth()>120)){
if (bi.getheight()>bi.getwidth())
ratio = 120.0/bi.getheight();
else
ratio = 120.0/bi.getwidth();
}

affinetransformop op = new affinetransformop(affinetransform.getscaleinstance(ratio, ratio), null);
itemp = op.filter(bi, null);
try {
imageio.write((bufferedimage)itemp, "jpg", thf);
}
catch (exception ex) {
throw new exception(" imageio.write error in creatthum.: "+ex.getmessage());
}
return (true);
}

public static void main(string[] args)
{
uploadimg ui;
boolean ss = false;

try{
ui = new uploadimg( "d:\\javalearn", "d:\\javalearn", "ps_high.jpg", "ps_low" );
ss = ui.createthumbnail();
if ( ss ) {
system.out.println( "success" );
}
else{
system.out.println( "error" );
}
}
catch(exception e){
system.out.print(e.tostring());
}
}
}
  • 打赏
  • 举报
回复
帮帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮 帮

顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶?????????顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶???????????????顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶??????????????????顶顶
顶顶顶顶顶顶顶顶顶顶顶????????????????顶顶顶顶顶顶顶顶
顶顶顶顶顶顶顶顶???????顶???顶顶?????顶顶顶顶顶顶顶顶顶
顶顶顶????????????顶顶顶顶顶顶????顶顶顶顶顶顶顶顶顶顶
顶??????????????顶顶顶顶顶顶????顶顶顶顶顶顶顶顶顶顶
顶????????????顶顶顶顶顶顶顶???????????顶顶顶顶
顶????????????顶顶顶顶顶顶??????????????顶顶
顶顶??????????顶顶顶顶顶??????顶顶顶???????顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶顶顶顶顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶??顶顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶????顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶????顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶???顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶顶???顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶????顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶????顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶????顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶????顶????顶顶?????顶顶顶
顶顶顶顶顶顶顶顶????顶顶顶顶???顶顶????顶顶?????顶顶顶
顶顶??顶顶顶?????顶顶顶顶???顶顶???顶顶顶?????顶顶顶
顶顶??????????顶顶顶顶顶??顶顶??顶顶顶顶?????顶顶顶
顶顶顶?????????顶顶顶顶顶顶顶顶???顶顶顶顶顶????顶顶顶
顶顶顶顶顶???????顶顶顶顶顶顶顶顶???顶????顶顶顶顶顶顶顶
顶顶顶顶顶顶??????顶顶顶顶顶顶顶????顶顶?????顶顶顶顶顶
顶顶顶顶顶顶顶顶顶???顶顶顶顶顶顶?????顶顶顶???????顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶??????顶顶顶顶顶??????顶顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶??????顶顶顶顶顶顶???????顶
顶顶顶顶顶顶顶顶顶顶顶顶顶顶?????顶顶顶顶顶顶顶顶顶??????顶
顶顶顶顶顶顶顶顶顶顶顶顶顶?????顶顶顶顶顶顶顶顶顶顶顶????顶顶
  • 打赏
  • 举报
回复
itisonlylove_yy 2009-09-23
学习一下,顺便接分
  • 打赏
  • 举报
回复
加载更多回复(79)
发帖
Web 开发

8.0w+

社区成员

Java Web 开发
社区管理员
  • Web 开发社区
加入社区
帖子事件
创建了帖子
2009-08-27 10:33
社区公告
暂无公告