zoukankan      html  css  js  c++  java
  • java 模拟ajax上传图片

    1、maven 引入依赖

      

    	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>
    	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.9</version>
    </dependency>
    

      

     2、

    @RequestMapping("upload")
    	public ResultMessage upload(HttpServletRequest request,HttpServletResponse response,
    			@RequestParam(name="userid",required=false)String userid,
    			@RequestParam(name="bid",required=false)String bid,
    			@RequestParam(name="pid",required=false)String pid,
    			@RequestParam(name="reportNumber",required=false)String reportNumber,
    			@RequestParam(name="plateNumber",required=false)String plateNumber,
    			@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
    		String filename=file.getOriginalFilename();
        	String extensionName = filename.substring(filename.lastIndexOf(".") + 1);
        	String imgPath = System.currentTimeMillis() + "." +extensionName;
            String filePath = request.getSession().getServletContext().getRealPath("/")+"uploadimages\";
            File newfile =  new File(filePath);
            if(!newfile.exists()){
            	newfile.mkdirs();
            }
            file.transferTo(new File(filePath+imgPath));
    		HttpClient client = new DefaultHttpClient();
    		HttpPost post= new HttpPost("");
    		MultipartEntity muti = new MultipartEntity();
    		File newfile1 = new File(filePath+imgPath);
    		FileBody body = new FileBody(newfile1);
    		muti.addPart("file",body);
    		muti.addPart("userid", new StringBody(userid));
    		muti.addPart("pid", new StringBody(pid));
    		muti.addPart("bid", new StringBody(bid));
    		muti.addPart("reportNumber", new StringBody(reportNumber));
    		muti.addPart("plateNumber", new StringBody(plateNumber));
    		post.setEntity(muti);
    		try {
    			HttpResponse resp = client.execute(post);
    			if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
    				return ResultMessage.getSuccess().setData(EntityUtils.toString(resp.getEntity()));
    			}
    		}catch (IOException e) {
    			logger.info(e.getMessage(),e);
    		}
    		return ResultMessage.getFail();
    		
    	}

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --><dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpmime</artifactId>    <version>4.5.2</version></dependency>

  • 相关阅读:
    [基础规范]JavaBeans规范
    leetcode 114.Flatten Binary Tree to Linked List (将二叉树转换链表) 解题思路和方法
    sql 分组取每组的前n条或每组的n%(百分之n)的数据
    D3js-API介绍【中】
    微信公众平台开发 一 账号类别与申请
    Apple Swift编程语言新手教程
    iOS中xib与storyboard原理,与Android界面布局的异同
    Scala入门到精通——第十五节 Case Class与模式匹配(二)
    使用IDA破解TraceMe.exe
    21行python代码实现拼写检查器
  • 原文地址:https://www.cnblogs.com/xdcr/p/9287937.html
Copyright © 2011-2022 走看看