栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java http上传接收文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java http上传接收文件

commons-httpclient commons-httpclient 3.1
    
        org.apache.httpcomponents
        httpclient
        4.5.10
    

    
        org.springframework.boot
        spring-boot-parent
        2.2.2.RELEASE
    

    
        org.springframework.boot
        spring-boot-starter-web
        2.2.2.RELEASE
    
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

public class FileLoadClient {
	
	private static final int TIMEOUT = 5000;
	
	public static void main(String[] args) {
		fileload("http://localhost:8080/uploadFile", new File("D:\google-collections\1.0\google-collections-1.0.jar"));
	}
	
	public static String fileload(String url, File file) {
		String body = "{}";
		
		if (url == null || url.equals("")) {
			return "illegal";
		}
		if (!file.exists()) {
			return "file not exist";
		}
		
		PostMethod postMethod = new PostMethod(url);
		BufferedReader br = null;
		try {
			// FilePart:用来上传文件的类,file即要上传的文件
			FilePart fp = new FilePart("file", file);
			Part[] parts = {fp};
			
			// 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
			MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
			postMethod.setRequestEntity(mre);
			
			HttpClient client = new HttpClient();
			// 由于要上传的文件可能比较大 , 因此在此设置最大的连接超时时间
			client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
			int status = client.executeMethod(postMethod);
			if (status == HttpStatus.SC_OK) {
				InputStream inputStream = postMethod.getResponseBodyAsStream();
				br = new BufferedReader(new InputStreamReader(inputStream));
				StringBuffer stringBuffer = new StringBuffer();
				String str = "";
				while ((str = br.readLine()) != null) {
					stringBuffer.append(str);
				}
				body = stringBuffer.toString();
			} else {
				body = "fail";
			}
		} catch (Exception e) {
//            。。。
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
//					。。。
				}
			// 释放连接
			postMethod.releaseConnection();
		}
		return body;
	}
}


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.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class UploadFile {
	
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public String uploadFile(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
		if (file != null && !file.isEmpty()) {
			try {
				String filePath = file.getOriginalFilename();
				InputStream inputStream = null;
				FileOutputStream outputStream = null;
				try {
					inputStream = file.getInputStream();
					File f = new File(filePath);
					f.createNewFile();
					byte[] bom = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
					outputStream = new FileOutputStream(f);
					if (filePath.endsWith(".txt")) {
						outputStream.write(bom);//写入BOM头
					}
					byte temp[] = new byte[1024];
					int size = -1;
					while ((size = inputStream.read(temp)) != -1) { // 每次读取1KB,直至读完
						outputStream.write(temp, 0, size);
					}
					response.getWriter().write("upload success");
				} catch (IOException e) {
					return e.getMessage();
				} finally {
					outputStream.close();
					inputStream.close();
				}
			} catch (Exception e) {
				return e.getMessage();
			}
		}
		return "File is Empty";
	}
	
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号