MultipartFile实现附件上传功能

一)服务端附件上传代码

package com.oysept.attachment.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件上传controller
 * @author ouyangjun
 */
@Controller
@RequestMapping(value = "/attachment")
public class UploadController {

    /**
     * 服务器附件上传接口
     * @param files
     * @param fileName
     * @return
     * 请求地址: http://localhost:8080/attachment/upload/fileUpload
     */
    @RequestMapping(value = "/upload/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public String fileUpload(@RequestParam(value="files") MultipartFile files, @RequestParam(value="fileName") String fileName) {
		
        // 附件名称
        System.out.println("AAAAAAAAAA" + fileName);
        try {
            // 流
            System.out.println("BBBBBBBBBB" + files.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 具体业务逻辑可行编写
        return "SUCCESS";
    }
}

 

二)客户端附件上传功能

测试方法:

package com.oysept.attachment.test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;

/**
 * 附件上传测试
 * @author ouyangjun
 */
public class UploadTest {

    public static void main(String[] args) {
		
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
	    	
            // 附件上传的地址
            HttpPost httpPost =new HttpPost("http://localhost:8080/attachment/upload/fileUpload");
	        
            // 把流转换成对象
            File file =new File("D:\\logo.png");
            FileBody fileBody =new FileBody(file);
	        
            StringBody fileName =new StringBody("附件名称", ContentType.create("text/plain", Consts.UTF_8));
	        
            // 封装实体
            // 设置浏览器兼容模式,解决文件名乱码问题(HttpMultipartMode.BROWSER_COMPATIBLE)
	        HttpEntity requestEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addPart("files", fileBody)//对应服务器的MultipartFile
                    .addPart("fileName", fileName)// 对应服务器的String
                    .setCharset(CharsetUtils.get("UTF-8")).build();
	 
            httpPost.setEntity(requestEntity);
	        
            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                // 获取响应对象
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity !=null) {
                    // 内容
                    System.out.println("print----: "+ EntityUtils.toString(responseEntity, Charset.forName("UTF-8")));
                }
                // 关闭
                EntityUtils.consume(responseEntity);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

识别二维码关注个人微信公众号

本章完结,待续,欢迎转载!
 
本文说明:该文章属于原创,如需转载,请标明文章转载来源!

你可能感兴趣的:(Java随作)