zoukankan      html  css  js  c++  java
  • restlet上传图片代码

    客户端用的是 jquery file upload, 服务器端用的是restlet
    package org.soachina.rest.component.resource;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.restlet.ext.fileupload.RestletFileUpload;
    import org.restlet.representation.Representation;
    import org.restlet.resource.Post;
    import org.restlet.resource.ServerResource;
    
    public class PostDemoResource extends ServerResource {
    	@Post
    	public Representation post(Representation entity) {
    		//        getResponse().setStatus(Status.SUCCESS_OK);  
    		//        return new StringRepresentation("11111111111" + parameters); 
    		Representation rep = null;
    		// 1/ Create a factory for disk-based file items
    		DiskFileItemFactory factory = new DiskFileItemFactory();
    		//System.out.println("大头");
    		// 2/ Create a new file upload handler based on the Restlet
    		// FileUpload extension that will parse Restlet requests and
    		// generates FileItems.
    		RestletFileUpload upload = new RestletFileUpload(factory);
    		
    		// 3/ Request is parsed by the handler which generates a
    		// list of FileItems
    		List<FileItem> items = null;
    		try {
    			items = upload.parseRepresentation(entity);
    		} catch (FileUploadException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
            
    		String filename =  "";
    		
    		for (FileItem fi : items) {
    			filename = fi.getName();
    			System.out.println("Save image ... " + filename);
    			/*新建一个图片文件*/
    			String extName=filename.substring(filename.lastIndexOf("."));
    	        String newName=new SimpleDateFormat("yyyyMMDDHHmmssms").format(new Date());
    	        File file=new File(newName+extName);  
    	        if(!file.exists()){//判断文件是否存在  
    	            try {  
    	                file.createNewFile();  //创建文件  
    	                  
    	            } catch (IOException e) {  
    	                // TODO Auto-generated catch block  
    	                e.printStackTrace();  
    	            }  
    	        }  
    	        /*获取文件路径*/  
    	        String path_=file.getPath();  
    	        /*获取绝对路径名*/  
    	        String absPath=file.getAbsolutePath();  
    	        /*获取父亲文件路径*/  
    	        String parent=file.getParent();  
    			try {
    				fi.write(file);
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    
    		}
    		return null;
    	}
    }

  • 相关阅读:
    php字符串处理函数大全 转
    php http Manual
    HTTP协议详解 百度文库
    PHP获取指定日期的上个月的日期 转
    计算指定日期的前N个月日期
    MySQL之count(*)与count(id)效率比较<转>
    生成器
    可迭代对象_迭代器
    变量内容的删除替换和替代
    软件磁盘阵列
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090517.html
Copyright © 2011-2022 走看看