Activiti工作流资源的获取和存储实现
在工作流Activiti中流程xml文件和流程图片的存储和实现:
在工作流部署的时候需要上传相关的工作流文件到相关的项目中。其中如果是文件采用方式如下,将图片和或者文件转换为二进制字节流存储。
存储的表结构如下:
create table ACT_GE_BYTEARRAY (
ID_ varchar(64) not null,
REV_ integer,
NAME_ varchar(255),
DEPLOYMENT_ID_ varchar(64),
BYTES_ BLOB,
primary key (ID_)
);
其中NAME_存储文件名称。
BYTES_存储流程资源文件。
代码实现如下:
package org.activiti.engine.identity;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
* @author Tom Baeyens
*/
public class Picture {
protected byte[] bytes;
protected String mimeType;
public Picture(byte[] bytes, String mimeType) {
this.bytes = bytes;
this.mimeType = mimeType;
}
public byte[] getBytes() {
return bytes;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}
public String getMimeType() {
return mimeType;
}
}
package org.activiti.rest.api.identity;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.identity.Picture;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.SecuredResource;
import org.restlet.data.CacheDirective;
import org.restlet.data.MediaType;
import org.restlet.representation.InputRepresentation;
import org.restlet.resource.Get;
/**
* @author Tijs Rademakers
*/
public class UserPictureResource extends SecuredResource {
@Get
public InputRepresentation getPicture() {
if(authenticate() == false) return null;
String userId = (String) getRequest().getAttributes().get("userId");
if(userId == null) {
throw new ActivitiException("No userId provided");
}
Picture picture = ActivitiUtil.getIdentityService().getUserPicture(userId);
String contentType = picture.getMimeType();
MediaType mediatType = MediaType.IMAGE_PNG;
if(contentType != null) {
if(contentType.contains(";")) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
mediatType = MediaType.valueOf(contentType);
}
InputRepresentation output = new InputRepresentation(picture.getInputStream(), mediatType);
getResponse().getCacheDirectives().add(CacheDirective.maxAge(28800));
return output;
}
}
package org.activiti.explorer.util;
import java.awt.image.BufferedImage;
/**
* @author Joram Barrez
*/
public class ImageUtil {
protected static final Logger LOGGER = Logger.getLogger(ImageUtil.class.getName());
/**
* Resizes the given image (passed as {@link InputStream}.
* If the image is smaller then the given maximum width or height, the image
* will be proportionally resized.
*/
public static InputStream smallify(InputStream imageInputStream, String mimeType, int maxWidth, int maxHeight) {
try {
BufferedImage image = ImageIO.read(imageInputStream);
int width = Math.min(image.getWidth(), maxWidth);
int height = Math.min(image.getHeight(), maxHeight);
Mode mode = Mode.AUTOMATIC;
if (image.getHeight() > maxHeight) {
mode = Mode.FIT_TO_HEIGHT;
}
if (width != image.getWidth() || height != image.getHeight()) {
image = Scalr.resize(image, mode, width, height);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, Constants.MIMETYPE_EXTENSION_MAPPING.get(mimeType), bos);
return new ByteArrayInputStream(bos.toByteArray());
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception while resizing image", e);
return null;
}
}
}