package com.freewebsys.core.action;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
@SuppressWarnings("serial")
public class SwfUploadAction extends BaseAction {
private File swfu;
private String swfuFileName;
private String swfuContentTypes;
private String filePath;// 上传文件路径
private String isResize;// 是否进行裁剪
private Integer resizeWidth;// 裁剪宽度
private Integer resizeHeight;// 裁剪高度
/**
* 直接将 ajax 输入到页面
*/
public synchronized void swfUploadFile() throws Exception {
System.out.println(swfuFileName);
// 获得webRoot的路径.
ServletContext context = ServletActionContext.getServletContext();
String filePathBase = context.getRealPath("/");
// 上传文件夹.
DateFormat format = new SimpleDateFormat("yyyy/MM/dd/");
// 设置上传路径upload/类型/年/月/日/
String filePathTemp = "upload/" + filePath + "/"
+ format.format(new Date());
String filePathTempResize = filePathTemp;
// 获得文件后缀名称
int position = swfuFileName.lastIndexOf(".");
String extension = swfuFileName.substring(position);
// 添加文件名称
Thread.sleep(1);// 防止文件名称重复
long time = System.currentTimeMillis();// 时间必须一致两个文件名称一致
filePathTemp += time + extension;
filePathTempResize += time + "_resize" + extension;
// System.out.println(System.currentTimeMillis());
// 判断文件夹
File tempFile = new File(filePathTemp);
if (!tempFile.isDirectory()) {// 如果目录不存在创建
try {
tempFile.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
// 拷贝文件
FileUtils.copyFile(swfu, new File(filePathBase + filePathTemp));
// 对图片文件进行等比例缩放。多余部分补白
if (isResize != null && isResize.equals("true") && resizeWidth != null
&& resizeHeight != null) {
imageResize(resizeWidth, resizeHeight, filePathBase + filePathTemp,
filePathBase + filePathTempResize);
}
// 返回上传文件路径和缩放图片路径
String json = "{'status':'true','fileName':'" + filePathTemp
+ "','fileNameResize':'" + filePathTempResize + "'}";
writeToPage(json);
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread.sleep(1);
System.out.println(System.currentTimeMillis());
// System.out.println(new Random().nextInt(10000));
}
String fileName = "src/images/test.jpg";
imageResize(200, 100, "src/images/test.jpg", "src/images/test_new.jpg");
System.out.println("finish.");
// System.out.println(new File().getName());
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
String fileNamePath = fileName.substring(0, position);
System.out.println(extension);
System.out.println(fileNamePath);
}
/**
* 图像缩放 jpg格式
*
* @param imgsrc
* 原图片文件路径
* @param imgdist
* 生成的缩略图片文件路径
* @param widthdist
* 生成图片的宽度
* @param heightdist
* 生成图片的高度
*/
public static void imageResize(int widthdist, int heightdist,
String imgsrc, String imgdist) {
try {
// int widthdist = 135;
// int heightdist = 90;
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = ImageIO.read(srcfile);
int width = src.getWidth(null);
int height = src.getHeight(null);
double rate = 0.0;
int x = 0;
int y = 0;
// 判断图像是否超宽超高,然后决定缩放比例,生成缩放图片.
if (((double) width / (double) height) > ((double) widthdist / (double) heightdist)) {
// 原始图片宽高比例 大于 规定比例,也就是超宽了
rate = (double) widthdist / (double) width;
y = (int) ((heightdist - rate * height) / 2);
} else {
rate = (double) heightdist / (double) height;
x = (int) ((widthdist - rate * width) / 2);
}
BufferedImage tag = new BufferedImage((int) widthdist,
(int) heightdist, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < widthdist; ++i)// 把原图片的内容复制到新的图片,同时把背景设为透明
{
for (int j = 0; j < heightdist; ++j) {
tag.setRGB(i, j, 0x00ffffff);// 这里把背景设为透明
}
}
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(
src.getScaledInstance((int) (width * rate),
(int) (height * rate), Image.SCALE_SMOOTH), x, y,
null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} catch (IOException ex) {
System.out.println("处理图片异常.");
// ex.printStackTrace();
}
}
public File getSwfu() {
return swfu;
}
public void setSwfu(File swfu) {
this.swfu = swfu;
}
public String getSwfuFileName() {
return swfuFileName;
}
public void setSwfuFileName(String swfuFileName) {
this.swfuFileName = swfuFileName;
}
public String getSwfuContentTypes() {
return swfuContentTypes;
}
public void setSwfuContentTypes(String swfuContentTypes) {
this.swfuContentTypes = swfuContentTypes;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getIsResize() {
return isResize;
}
public void setIsResize(String isResize) {
this.isResize = isResize;
}
public Integer getResizeWidth() {
return resizeWidth;
}
public void setResizeWidth(Integer resizeWidth) {
this.resizeWidth = resizeWidth;
}
public Integer getResizeHeight() {
return resizeHeight;
}
public void setResizeHeight(Integer resizeHeight) {
this.resizeHeight = resizeHeight;
}
}