上傳圖片的jsp頁面 javascript
<style type="text/css">
input {
vertical-align: middle;
margin: 0;
padding: 0
}
.file-box {
position: relative;
width: 340px
}
.txt {
height: 22px;
border: 1px solid #cdcdcd;
width: 180px;
}
.btn {
background-color: #FFF;
border: 1px solid #CDCDCD;
height: 24px;
width: 70px;
}
.file {
position: absolute;
top: 0;
right: 80px;
height: 24px;
filter: alpha(opacity : 0);
opacity: 0;
width: 260px
}
/* 定製文件上傳按鈕 */
.u-upload {
display: inline-block;
*display: inline;
*zoom: 1;
position: relative;
overflow: hidden;
}
.u-upload button {
height: 22px;
padding: 0 10px;
border: 1px solid #ccc;
overflow: visible;
font-size: 14px;
color: #666;
background: #ddd;
}
.u-upload input {
position: absolute;
top: 0;
right: -1px;
font-size: 100px;
cursor: pointer;
opacity: 0;
filter: alpha(opacity = 0);
}
.u-upload:hover button {
border-color: #bbb;
color: #333;
background: #ccc;
}
a.u-upload,a.u-upload:hover {
text-decoration: none;
}
</style> css
<script type='text/javascript'>
var file = $("#file").val();
var fileName = getFileName(file);
function getFileName(o) {
var pos = o.lastIndexOf("\\");
return o.substring(pos + 1);
}
<%--名字不要起upload!!!名字衝突!--%>
function upload1() {
document.commentform.action = "upload!upload";
document.commentform.submit();
}
function publish() {
document.commentform.action = "secondHandAction!putmessage";
document.commentform.submit();
}
</script> html
<form method="post" id="commentform" name="commentform"
class="form" action="" ENCTYPE="multipart/form-data">
<textarea name="comment" type="text" placeholder="信息內容" rows="15"
class="span8" tabindex="4" aria-required='true'></textarea>
<input type='text' name='uploadFileName' id='uploadFileName'
class='txt' value="<%=Util.uploadFileUrl%>"/> <a href="#" class="u-upload">
<button type="button">瀏覽..</button> <input type="file"
name="upload" class="file" id="upload" size="28"
onchange="document.getElementById('uploadFileName').value=this.value" />
</a> <input name="submit" type="submit" value="上傳"
class="btn btn-small btn-info" onClick="upload1()" /> </br>
</form> 前端
上傳圖片的Action類 java
package com.efinance.action; 數據庫
裁剪圖片 apache
package com.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;
import org.apache.struts2.ServletActionContext;
/**
*@author island
*@date 2011-08-13
*/
public class OperateImage {
//===源圖片路徑名稱如:c:\1.jpg
private String srcpath ;
//===剪切圖片存放路徑名稱.如:c:\2.jpg
private String subpath ;
//===剪切點x座標
private int x ;
private int y ;
//===剪切點寬度
private int width ;
private int height ;
public OperateImage(){
}
public OperateImage(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);
/** *//**//*
* 返回包含全部當前已註冊 ImageReader 的 Iterator,這些 ImageReader
* 聲稱可以解碼指定格式。 參數:formatName - 包含非正式格式名稱 .
*(例如 "jpeg" 或 "tiff")等 。
*/
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = it.next();
//獲取圖片流
iis = ImageIO.createImageInputStream(is);
/** *//**//*
* <p>iis:讀取源.true:只向前搜索 </p>.將它標記爲 ‘只向前搜索’。
* 此設置意味着包含在輸入源中的圖像將只按順序讀取,可能容許 reader
* 避免緩存包含與之前已經讀取的圖像關聯的數據的那些輸入部分。
*/
reader.setInput(iis,true) ;
/** *//**//*
* <p>描述如何對流進行解碼的類<p>.用於指定如何在輸入時從 Java Image I/O
* 框架的上下文中的流轉換一幅圖像或一組圖像。用於特定圖像格式的插件
* 將從其 ImageReader 實現的 getDefaultReadParam 方法中返回
* ImageReadParam 的實例。
*/
ImageReadParam param = reader.getDefaultReadParam();
/** *//**//*
* 圖片裁剪區域。Rectangle 指定了座標空間中的一個區域,經過 Rectangle 對象
* 的左上頂點的座標(x,y)、寬度和高度能夠定義這個區域。
*/
Rectangle rect = new Rectangle(x, y, width, height);
//提供一個 BufferedImage,將其用做解碼像素數據的目標。
param.setSourceRegion(rect);
/** *//**//*
* 使用所提供的 ImageReadParam 讀取經過索引 imageIndex 指定的對象,並將
* 它做爲一個完整的 BufferedImage 返回。
*/
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 = "d:\\imges.jpg";
OperateImage o = new OperateImage(220,213,200,333);
o.setSrcpath(name);
o.setSubpath("D:\\2.jpg");
o.cut() ;
}
} 服務器
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title></title>
<script src="<%=basePath %>/js/prototype.js" type="text/javascript"></script>
<script src="<%=basePath %>/js/scriptaculous.js?load=builder,dragdrop" type="text/javascript"></script>
<script src="<%=basePath %>/js/cropper.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath %>/css/cropper.css" media="all" />
<script type="text/javascript" charset="utf-8">
function onEndCrop( coords, dimensions ) {
$( 'x1' ).value = coords.x1;
$( 'y1' ).value = coords.y1;
$( 'x2' ).value = coords.x2;
$( 'y2' ).value = coords.y2;
$( 'width' ).value = dimensions.width;
$( 'height' ).value = dimensions.height;
}
// example with a preview of crop results, must have minimumm dimensions
Event.observe(
window,
'load',
function() {
new Cropper.ImgWithPreview(
'testImage',
{
minWidth: 100,
minHeight: 100,
ratioDim: { x: 100, y: 100 },
displayOnInit: true,
onEndCrop: onEndCrop,
previewWrap: 'previewArea'
}
)
}
);
/*
if( typeof(dump) != 'function' ) {
Debug.init(true, '/');
function dump( msg ) {
// Debug.raise( msg );
};
} else dump( '---------------------------------------\n' );
*/
</script>
<link rel="stylesheet" type="text/css" href="debug.css" media="all" />
<style type="text/css">
label {
clear: left;
margin-left: 50px;
float: left;
width: 5em;
}
#testWrap {
width: 500px;
float: left;
margin: 20px 0 0 50px;
}
#previewArea {
margin: 20px; 0 0 20px;
float: left;
}
#results {
clear: both;
}
</style>
</head>
<body>
<form action="<%=basePath %>cut.action" method="post">
<br /><br />
<!-- 組件要顯示的圖片 -->
<div id="testWrap">
<img id="testImage" src ='upload/<s:property value ="uploadFileName" /> ' />
<br />
<s:property value ="caption" />
</div>
<div id="previewArea"></div>
<div id="results">
<p>
<label for="x1">x1:</label>
<input type="text" name="x1" id="x1" />
</p>
<p>
<label for="y1">y1:</label>
<input type="text" name="y1" id="y1" />
</p>
<p>
<label for="x2">x2:</label>
<input type="text" name="x2" id="x2" />
</p>
<p>
<label for="y2">y2:</label>
<input type="text" name="y2" id="y2" />
</p>
<p>
<label for="width">width:</label>
<input type="text" name="width" id="width" />
</p>
<p>
<label for="height">height</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="hidimg" value="<s:property value ="uploadFileName" />"/>
</p>
</div>
<input type="submit" value="剪切"/>
</form>
</body>
</html> 框架
工具類
public class Util { /** * 存放文件名 */ public static String uploadFileName=""; /** * 存放文件完整路徑 */ public static String uploadFileUrl=""; public static int userId=0; public static String number=""; }