Author: andrei_exadel
Date: 2009-01-30 12:54:05 -0500 (Fri, 30 Jan 2009)
New Revision: 12508
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileItem.java
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileUploadBean.java
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/ImageLoader.java
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java
trunk/test-applications/realworld/web/src/main/webapp/WEB-INF/web.xml
trunk/test-applications/realworld/web/src/main/webapp/includes/fileUpload/multyMode.xhtml
Log:
Upload images to temp files
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileItem.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileItem.java 2009-01-30
17:17:05 UTC (rev 12507)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileItem.java 2009-01-30
17:54:05 UTC (rev 12508)
@@ -20,6 +20,8 @@
*/
package org.richfaces.realworld.fileupload;
+import java.io.File;
+
import org.richfaces.realworld.domain.Image;
public class FileItem {
@@ -32,6 +34,8 @@
private byte[] data;
+ private File file;
+
public byte[] getData() {
return data;
}
@@ -61,4 +65,12 @@
public void setImage(Image image) {
this.image = image;
}
+ public File getFile() {
+ return file;
+ }
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+
}
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java 2009-01-30
17:17:05 UTC (rev 12507)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileManager.java 2009-01-30
17:54:05 UTC (rev 12508)
@@ -30,7 +30,6 @@
import javax.imageio.ImageIO;
import org.jboss.seam.ScopeType;
-import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.richfaces.realworld.service.Constants;
@@ -133,13 +132,17 @@
public boolean addImage(String fileName, byte[] data) {
createDirectoryIfNotExist(fileName);
+ /*
try {
+
writeFileTodisk(data, fileName, 0, 0, "" );
writeFileTodisk(data, fileName, 100, 100, _MINI );
writeFileTodisk(data, fileName, 500, 500, _MEDIUM );
+
} catch (IOException e) {
return false;
}
+ */
return true;
}
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileUploadBean.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileUploadBean.java 2009-01-30
17:17:05 UTC (rev 12507)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/FileUploadBean.java 2009-01-30
17:54:05 UTC (rev 12508)
@@ -21,7 +21,10 @@
package org.richfaces.realworld.fileupload;
-import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
@@ -46,6 +49,7 @@
import org.richfaces.realworld.navigation.NavigationEnum;
import org.richfaces.realworld.service.Constants;
import org.richfaces.realworld.util.ConversationState;
+import org.richfaces.realworld.util.ImageUtils;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
@@ -62,7 +66,7 @@
@Name("fileUploadBean")
@Scope(ScopeType.CONVERSATION)
public class FileUploadBean implements Serializable {
-
+
@In
private User user;
@In("#{messages['file_processing_error']}")
@@ -96,11 +100,16 @@
public synchronized void listener(UploadEvent event) throws Exception {
UploadItem item = event.getUploadItem();
+
FileItem file = new FileItem();
- file.setLength(item.getData().length);
- file.setData(item.getData());
+
+ file.setLength(item.getFileSize());
+ //file.setData(item.getData());
+ file.setFile(item.getFile());
file.setSelected(false);
Image image = constructImage(item, file);
+
+ resizeImages(item, fileManager.getUploadRoot() + image.getPath());
try {
extractMetadata(file, image);
} catch (JpegProcessingException e1) {
@@ -114,6 +123,10 @@
fileWrapper.getFiles().add(file);
}
+ private void resizeImages(UploadItem src, String pathToUpload) throws IOException {
+ ImageUtils.resizeImage(src, pathToUpload, ImageUtils.MINI, ImageUtils.MEDIUM);
+ }
+
@Observer(Constants.FILE_UPLOAD_CLEAR_EVENT)
public void clearUploadData() {
fileWrapper.getFiles().clear();
@@ -218,7 +231,7 @@
image.setUploaded(new Date());
image.setDescription(SAMPLE_DESCRIPTION);
image.setName(SAMPLE_NAME);
- image.setSize(file.getLength() / Constants.KB);
+ image.setSize(file.getLength());
String albumPath =
conversationState.getSelectedAlbum().getAlbumPathFromParents(conversationState.getSelectedAlbum(),
Constants.SLASH, true);
image.setPath(user.getLogin() + Constants.SLASH + albumPath + item.getFileName());
@@ -243,8 +256,8 @@
}
private void extractMetadata(FileItem file, Image image)
- throws JpegProcessingException, MetadataException {
- InputStream in = new ByteArrayInputStream(file.getData());
+ throws JpegProcessingException, MetadataException, FileNotFoundException {
+ InputStream in = new FileInputStream(file.getFile());
Metadata metadata = JpegMetadataReader.readMetadata(in);
Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
Directory jpgDirectory = metadata.getDirectory(JpegDirectory.class);
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/ImageLoader.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/ImageLoader.java 2009-01-30
17:17:05 UTC (rev 12507)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/fileupload/ImageLoader.java 2009-01-30
17:54:05 UTC (rev 12508)
@@ -29,7 +29,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
-import org.richfaces.realworld.service.Constants;
import javax.imageio.ImageIO;
@@ -38,6 +37,7 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
+import org.richfaces.realworld.service.Constants;
@Name("imageLoader")
@Scope(ScopeType.CONVERSATION)
@@ -58,7 +58,16 @@
}else{
index = ((Long)data).intValue();
}
- stream.write(fileWrapper.getFiles().get(index).getData());
+ FileInputStream fileInputStream = new
FileInputStream(fileWrapper.getFiles().get(index).getFile());
+ int read = -1;
+ byte [] bytes = new byte [4096];
+ while ((read = fileInputStream.read(bytes)) != -1) {
+ if (read == 0) {
+ break;
+ }
+ stream.write(bytes);
+ }
+ //stream.write(fileWrapper.getFiles().get(index).getData());
}
public synchronized void paintSearchImage(OutputStream stream, Object object)
Modified:
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java
===================================================================
---
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java 2009-01-30
17:17:05 UTC (rev 12507)
+++
trunk/test-applications/realworld/web/src/main/java/org/richfaces/realworld/util/ImageUtils.java 2009-01-30
17:54:05 UTC (rev 12508)
@@ -4,17 +4,46 @@
package org.richfaces.realworld.util;
import java.awt.Color;
+import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
+import java.io.File;
import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.FileImageInputStream;
+import javax.imageio.stream.ImageInputStream;
+
+import org.richfaces.model.UploadItem;
+import org.richfaces.realworld.service.Constants;
+
/**
* @author Andrey Markavtsov
*
*/
public class ImageUtils {
+
+ public static class ImageDimension {
+ public int WIDTH;
+ public int HEIGHT;
+ public String POSTFIX_NAME;
+ public ImageDimension(int width, int height, String postfix_name) {
+ super();
+ WIDTH = width;
+ HEIGHT = height;
+ POSTFIX_NAME = postfix_name;
+ }
+ };
+
+ public static final String _MEDIUM = "_medium";
+ public static final String _MINI = "_mini";
+
+ public static ImageDimension MINI = new ImageDimension(100, 100, _MINI);
+ public static ImageDimension MEDIUM = new ImageDimension(640, 480, _MEDIUM);
+
+
BufferedImage bufferedImage;
double width;
@@ -24,30 +53,30 @@
if (bufferedImage == null) {
throw new NullPointerException("Buffered image is null");
}
-
+
this.height = bufferedImage.getHeight();
this.width = bufferedImage.getWidth();
this.bufferedImage = bufferedImage;
}
- private void scaleToWidth(int width) throws IOException {
+ private BufferedImage scaleToWidth(int width) throws IOException {
double height = width * this.height / this.width;
BufferedImage newImage = new BufferedImage(width, (int) height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = createGraphics(newImage);
graphics2D.drawImage(bufferedImage, 0, 0, (int) width, (int) height,
null);
- bufferedImage = newImage;
+ return newImage;
}
- private void scaleToHeight(int height) throws IOException {
+ private BufferedImage scaleToHeight(int height) throws IOException {
double width = height * this.width / this.height;
BufferedImage newImage = new BufferedImage((int) width, (int) height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = createGraphics(newImage);
graphics2D.drawImage(bufferedImage, 0, 0, (int) width, (int) height,
null);
- bufferedImage = newImage;
+ return newImage;
}
private Graphics2D createGraphics(BufferedImage image) {
@@ -61,20 +90,59 @@
public BufferedImage resizeImage(double width, double height)
throws IOException {
-
+
if (width > height) {
- if(this.bufferedImage.getWidth() < width){
+ if (this.bufferedImage.getWidth() < width) {
return bufferedImage;
}
- scaleToWidth((int) width);
+ return scaleToWidth((int) width);
} else {
- if(this.bufferedImage.getHeight() < height){
+ if (this.bufferedImage.getHeight() < height) {
return bufferedImage;
}
- scaleToHeight((int) height);
+ return scaleToHeight((int) height);
}
- return bufferedImage;
}
+ public static void resizeImage(UploadItem image, String pathToUpload, ImageDimension ...
dimensions)
+ throws IOException {
+
+ BufferedImage bufferedImage = ImageIO.read(new FileImageInputStream(image.getFile()));
+
+ ImageUtils utils = new ImageUtils(bufferedImage);
+ int width, height;
+
+ ImageIO.write(bufferedImage, Constants.JPG, new File(pathToUpload));
+
+ for (ImageDimension dimension : dimensions) {
+ String fileName = transformPath(pathToUpload,dimension.POSTFIX_NAME);
+ width = dimension.WIDTH;
+ height = dimension.HEIGHT;
+ BufferedImage resizedBuffer = bufferedImage;
+
+ if (width > height) {
+ if (bufferedImage.getWidth() > width) {
+ resizedBuffer = utils.scaleToWidth((int) width);
+ }
+ } else {
+ if (bufferedImage.getHeight() > height) {
+ resizedBuffer = utils.scaleToHeight((int) height);
+ }
+ }
+
+ if (resizedBuffer != null) {
+ ImageIO.write(resizedBuffer, Constants.JPG, new File(fileName));
+ }
+
+ }
+
+ }
+
+ public static String transformPath(String target, String substitute){
+ String begin = target.substring(0, target.lastIndexOf(Constants.DOT));
+ String end = target.substring(target.lastIndexOf(Constants.DOT));
+ return begin + substitute + end;
+ }
+
}
Modified: trunk/test-applications/realworld/web/src/main/webapp/WEB-INF/web.xml
===================================================================
--- trunk/test-applications/realworld/web/src/main/webapp/WEB-INF/web.xml 2009-01-30
17:17:05 UTC (rev 12507)
+++ trunk/test-applications/realworld/web/src/main/webapp/WEB-INF/web.xml 2009-01-30
17:54:05 UTC (rev 12508)
@@ -11,7 +11,7 @@
<filter-class>org.ajax4jsf.Filter</filter-class>
<init-param>
<param-name>createTempFiles</param-name>
- <param-value>false</param-value>
+ <param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxRequestSize</param-name>
@@ -81,7 +81,7 @@
<context-param>
<param-name>uploadRoot</param-name>
<param-value>
- /home/richfaces/Upload/
+ D:\Work\Projects\RichFaces\SVN\trunk\test-applications\realworld\Upload\
</param-value>
</context-param>
Modified:
trunk/test-applications/realworld/web/src/main/webapp/includes/fileUpload/multyMode.xhtml
===================================================================
(Binary files differ)