关于jersey实现文件上传下载功能

bjsq618 2013-05-29
如题,如何在系统中实现这两个功能?
redhacker 2013-05-30
上传可以直接用fileupload组件。其实上传下载跟普通在servlet里没什么两样,因为你在Resource类的方法里面可以取得request和response,这两个对象取到了,什么都可以解决了!举个上传例子:

@Path("/uploadFile")
public class UploadFileResource {
    
    /**
     * httpServletRequest
     */
    @Context
    private HttpServletRequest httpServletRequest;
    
    /**
     * 上传文件
     * @throws JSONException 
     */
    @SuppressWarnings("unchecked")
    @POST
    public Response uploadFile() {

        // 创建一个基于硬盘的FileItem工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 设置向硬盘写数据时所用的缓冲区的大小,此处为4K
        factory.setSizeThreshold(4 * 1024);

        // 创建一个文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 设置允许上传的文件的最大尺寸,此处为10M
        upload.setSizeMax(10 * 1024 * 1024);

        // FileItem
        List<FileItem> fileItemList = null;

        try {
            /**
             * 在web.xml里SpringServlet中配置了com.sun.jersey.spi.container.ContainerRequestFilters参数后
             */
            fileItemList = upload.parseRequest(httpServletRequest);
            System.out.println("fileItemList size:" + fileItemList.size());
        } catch (FileUploadException e) {
        }
        return Response.ok("{success:true}").status(Status.OK).build();
        
    }
}

raoda825 2013-11-29
下载功能怎么做。客户端怎么去到这个流文件
raoda825 2013-11-29
/**服务器端
	 * 下载当前最新的apk version
	 */
	@Path("/download")
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String download(@Context HttpServletRequest request,@Context HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		FileInputStream fis = null;
		BufferedInputStream buff = null;
		OutputStream myout = null;

		ApkService apkService = getBean();
		ApkVersion apkVersion = apkService.getNewApkVersion();
		String path = "E:\\svn project\\APP\\WebRoot\\WEB-INF\\download\\1.png";
		//String path = "//web" + File.separator + "WebRoot"+ File.separator + "WEB-INF" + File.separator + "download"+ File.separator + "1.txt";
		System.out.println(path+"==========================");
		File file = new File(path);

		try {
			if (!file.exists()) {
				response.sendError(404, "File not found!");
				return "";
			} else {
				response.setContentType("application/x-msdownload");
				response.setContentLength((int) file.length());
				response.setHeader("Content-Disposition","attachment;filename="+ new String(file.getName().getBytes("gbk"),"iso-8859-1"));
			}
			response.reset();
			fis = new FileInputStream(file);
			buff = new BufferedInputStream(fis);
			byte[] b = new byte[1024];
			long k = 0;
			myout = response.getOutputStream();

			while (k < file.length()) {
				int j = buff.read(b, 0, 1024);
				k += j;
				myout.write(b, 0, j);
			}
			myout.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (buff != null)
					buff.close();
				if (myout != null)
					myout.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "";
	}


	//客户端代码
	@SuppressWarnings("unchecked")
	public static String testVersion4(){
		Client client = Client.create();
		WebResource r = client.resource("http://localhost:8080/APP/rest/appversion/download");
		MultivaluedMap queryParams = new MultivaluedMapImpl();
		queryParams.add("", "");
		r.queryParams(queryParams).get(String.class);
		
		return "";
	}

客户端怎么得到这个流
Global site tag (gtag.js) - Google Analytics