急! 在线等待
解决方案 »
- java web页面请求结束一个正在运行任务
- 这种乱码问题谁能解决啊?
- 菜鸟求助
- 100分求一本JSP书籍,最后从基础开始的!谁可以给我传下,马上送分,非常感谢!!!!!!!!!!!!!!!!!!!
- 请问关于session.setAttribute()的问题
- 如何让一个帐号不能重复登录????
- connection.getRS():[Microsoft][ODBC Microsoft Access Driver] UPDATE 语句的语法错误。
- 请高手指点:quest对象和session对象的是用方法,最好有例子说明。(UP有分)
- 请教:缓存问题
- 请Jsp高手帮忙,我很急...
- jasperReport在linux下的问题
- 在线等关于文章心情投票问题 ~~万分迫切~
[email protected]
功能可能要扩展~
public class A {
/**
* @param args
*/
public static void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "jpg", new File(fileName));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="D:\\aa.jpg";
try {
captureScreen(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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");
}
} 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() ;
}
}
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;
} } 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;
%> <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"> © EduAsis. All rights reserved. Design by heyee. </div> </div> </div> </body>
</html>