Java tutorial
/** * Project: isor * * File Created at 2013-7-9 * $Id$ * * Copyright 2008 Shensuoyao.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Shensuoyao Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Shensuoyao.com. */ package com.ssy.isor.ws.impl; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.annotation.Resource; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.wink.common.internal.MultivaluedMapImpl; import org.apache.wink.common.model.multipart.BufferedInMultiPart; import org.apache.wink.common.model.multipart.InPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.ssy.isor.bean.IsorConfig; import com.ssy.isor.util.file.FileOperateUtil; import com.ssy.isor.util.file.FileUtils; import com.ssy.isor.util.image.ImageType; import com.ssy.isor.ws.IImageWS; /** * @author shensuoyao * @date 2013.07.09 */ @Path("/images") @Service("imageWS") public class ImageWS implements IImageWS { private Logger LOG = LoggerFactory.getLogger(this.getClass()); @Resource(name = "isorConfig") private IsorConfig isorConfig; /* (non-Javadoc) * @see com.ssy.isor.ws.IImageWS#getFile(java.lang.String, java.lang.String, java.lang.String) */ @Override public Response getImage(@PathParam("module") String module, @PathParam("fileId") String fileId, @QueryParam("filename") String filename) throws FileNotFoundException { try { String filePath = FileUtils.createFolder(isorConfig.getImagesRootPath() + module, fileId) + filename; File file = new File(filePath); if (file.exists()) { String fileExt = file.toString().substring(file.toString().lastIndexOf('.') + 1); // This default is standard for browsers MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE; if (fileExt.equalsIgnoreCase(ImageType.IMAGE_JPG) || fileExt.equalsIgnoreCase(ImageType.IMAGE_JPEG)) { mediaType = new MediaType("image", ImageType.IMAGE_JPG); } else if (fileExt.equalsIgnoreCase(ImageType.IMAGE_GIF)) { mediaType = new MediaType("image", ImageType.IMAGE_GIF); } else if (fileExt.equalsIgnoreCase(ImageType.IMAGE_PNG)) { mediaType = new MediaType("image", ImageType.IMAGE_PNG); } else if (fileExt.equalsIgnoreCase(ImageType.IMAGE_ZIP)) { mediaType = new MediaType("image", ImageType.IMAGE_ZIP); } return Response.ok().type(mediaType).entity(new FileInputStream(file)).build(); } } catch (IOException e) { LOG.error("create dest file path error.", e); } throw new WebApplicationException(Response.Status.NOT_FOUND); } /* (non-Javadoc) * @see com.ssy.isor.ws.IImageWS#uploadImage(java.lang.String, java.lang.String, org.apache.wink.common.model.multipart.BufferedInMultiPart) */ @Override public Response uploadImage(@Context HttpHeaders httpHeaders, @Context UriInfo uriInfo, @PathParam("module") String module, @PathParam("fileId") String fileId, BufferedInMultiPart bufferInMP) throws IOException { List<InPart> parts = bufferInMP.getParts(); JSONArray jsonArray = new JSONArray(); for (InPart inP : parts) { MultivaluedMap<String, String> headers = new MultivaluedMapImpl<String, String>(); headers = inP.getHeaders(); String fileName = headers.get("filename").get(0); if (fileName != null) { String destFilePath = FileUtils.createFolder(isorConfig.getImagesRootPath() + module, fileId) + fileName; byte[] bytes = FileOperateUtil.readInputStream(inP.getInputStream()); FileOperateUtil.writeByte2File(destFilePath, bytes); jsonArray.add(generateJSONObj(fileName, bytes.length, module, fileId)); } } return Response.ok(jsonArray.toString(), MediaType.APPLICATION_JSON).build(); } /* (non-Javadoc) * @see com.ssy.isor.ws.IImageWS#upload(java.io.InputStream) */ @Override public Response upload(InputStream input) throws IOException { byte[] bytes = FileOperateUtil.readInputStream(input); FileOperateUtil.writeByte2File("", bytes); return Response.ok().build(); } /** * ?JSON * @param fileName * @param fileSize * @param module * @param fileId * @return */ private JSONObject generateJSONObj(String fileName, long fileSize, String module, String fileId) { JSONObject jsonObject = new JSONObject(); jsonObject.put("fileName", fileName); jsonObject.put("fileSize", fileSize); jsonObject.put("module", module); jsonObject.put("fileId", fileId); return jsonObject; } }