wink rest 接口实现文件上传下载


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.Iterator;

import java.util.List;


import javax.servlet.http.HttpServletRequest;

import javax.ws.rs.GET;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.QueryParam;

import javax.ws.rs.core.Context;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.Status;


import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.lang.StringUtils;


@Path("/helloworld")

public class HelloWorldResource

{


File filesDir = new File(System.getProperty("user.home"));


@GET

@Path("/image")

public Response getImage(@QueryParam("userid") String userId)

{

if (StringUtils.isBlank(userId)) {

return Response.status(Status.BAD_REQUEST).build();

}

File f = new File(filesDir + File.separator + userId + ".jpg");

FileInputStream fis = null;

try {

fis = new FileInputStream(f);

byte b[] = new byte[(int) f.length()];

fis.read(b);

return Response.status(Status.OK).type("image/jpeg").entity(b).build();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return Response.status(Status.BAD_REQUEST).build();

}


@POST

@Path("/image")

public Response uploadImage(@QueryParam("userid") String userId,

@Context HttpServletRequest request)

{

if (StringUtils.isBlank(userId)) {

return Response.status(Status.BAD_REQUEST).build();

}

try {

request.setCharacterEncoding("UTF-8");

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

DiskFileItemFactory fileFactory = new DiskFileItemFactory();

fileFactory.setRepository(filesDir);

ServletFileUpload uploader = new ServletFileUpload(fileFactory);


if (!ServletFileUpload.isMultipartContent(request)) {

return Response.status(Status.BAD_REQUEST).build();

}

try {

List<FileItem> fileItemsList = uploader.parseRequest(request);

Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();

while (fileItemsIterator.hasNext()) {

FileItem fileItem = fileItemsIterator.next();

System.err.println("FieldName=" + fileItem.getFieldName());

System.err.println("FileName=" + fileItem.getName());

System.err.println("ContentType=" + fileItem.getContentType());

System.err.println("Size in bytes=" + fileItem.getSize());

if (StringUtils.isNotBlank(fileItem.getName())) {

File file = new File(filesDir + File.separator + fileItem.getName());

System.err.println("Absolute Path at server=" + file.getAbsolutePath());

fileItem.write(file);

}

}

return Response.status(Status.OK).entity("upload success").build();

} catch (FileUploadException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return Response.status(Status.NOT_FOUND).build();

}


}


转载于:https://my.oschina.net/liuyuxiang/blog/202755

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值