package com.itrus.controller;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author jiangwenhui
* @Description
* @Date Created in 2017/12/25 0025
*/
@RestController
@RequestMapping("/file")
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
//请求路径
private static final String url = "http://localhost:8080/file-service-demo/file/upload";
//文件分隔符
private static final String separator = File.separator;
//media type
private static final MediaType MEDIA_TYPE = MediaType.parse("media/type");
//okhttp-client
private final OkHttpClient client = new OkHttpClient();
//编码
public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
/**
* 本地单文件上传
*/
public void uploadFileLocalSingle() {
//获取文件
File file = new File("E:" + separator +"testFileUpload"+ separator + "eclipse-jee-luna-SR1-win32-x86_64.zip");
//设置请求body
RequestBody body = new MultipartBody.Builder()
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("media/type"), file))
.setType(MultipartBody.FORM)
.build();
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
final String startTime = df.format(new Date());
System.out.println("当前时间" + startTime);// new Date()为获取当前系统时间
//返回相应
Response response = null;
try {
//创建请求
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//发起请求
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
public void onResponse(Call call, Response response){
if (!response.isSuccessful()) {
System.out.println("Unexpected code " + response.code());
} else {
ResponseBody responseBody = response.body();
if(null != responseBody){
System.out.println("上传成功");
final String endTime = df.format(new Date());
System.out.println("结束时间 " + endTime);
}else {
System.out.println("responseBody is null");
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != response){
response.close();
}
}
}
/**
* 本地多文件上传
*/
public void uploadFileMult(){
//获取文件
File file1 = new File("E:" + separator + "testFileUpload" + "171221线上快裁业务流程V1.5(1).pdf");
File file2 = new File("E:" + separator + "testFileUpload" + "test1.txt");
File file3 = new File("E:" + separator + "testFileUpload" + "test2.txt");
RequestBody filebody1 = RequestBody.create(MEDIA_TYPE,file1);
RequestBody filebody2 = RequestBody.create(MEDIA_TYPE,file2);
RequestBody filebody3 = RequestBody.create(MEDIA_TYPE,file3);
MultipartBody mBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file" , file1.getName() , filebody1)
.addFormDataPart("file" , file2.getName() , filebody2)
.addFormDataPart("file" , file3.getName(), filebody3)
.build();
//返回相应
Response response = null;
try {
//创建请求
Request request = new Request.Builder()
.url(url)
.post(mBody)
.build();
//发起请求
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
}
public void onResponse(Call call, Response response){
if (!response.isSuccessful()) {
System.out.println("Unexpected code " + response.code());
} else {
ResponseBody responseBody = response.body();
if(null != responseBody){
System.out.println(responseBody.toString());
}else {
System.out.println("responseBody is null");
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != response){
response.close();
}
}
}
public static void main(String[] args) {
FileController fileController = new FileController();
//fileController.uploadFileMult(); //多文件
fileController.uploadFileLocalSingle(); //单文件
}
@RequestMapping("/upload")
public String upload(@RequestParam("allFile") CommonsMultipartFile[] files, HttpServletRequest request){
for(int i = 0;i<files.length;i++){
System.out.println("fileName---------->" + files[i].getOriginalFilename());
CommonsMultipartFile commonsMultipartFiles = files[i];
try {
InputStream inputStream = commonsMultipartFiles.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
if(!files[i].isEmpty()){
int pre = (int) System.currentTimeMillis();
try {
//拿到输出流,同时重命名上传的文件
FileOutputStream os = new FileOutputStream("E:/" + System.currentTimeMillis() + files[i].getOriginalFilename());
//拿到上传文件的输入流
FileInputStream in = (FileInputStream) files[i].getInputStream();
//以写字节的方式写文件
int b = 0;
while((b=in.read()) != -1){
os.write(b);
}
os.flush();
os.close();
in.close();
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传出错");
}
}
}
return "/success";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
http上传文件客户端和服务端代码
共171个文件
xml:81个
jar:43个
class:18个
4星 · 超过85%的资源 需积分: 31 66 下载量 92 浏览量
2018-01-29
11:19:08
上传
评论 1
收藏 17.17MB ZIP 举报
温馨提示
http上传文件客户端和服务端代码,可以做个参考,比较详细。也可以参看我的博客http://blog.csdn.net/qiumeng_1314/article/details/79192766
资源推荐
资源详情
资源评论
收起资源包目录
http上传文件客户端和服务端代码 (171个子文件)
FileController.class 6KB
FileController.class 6KB
FileUploadController.class 4KB
FileUploadController.class 4KB
FileController.class 4KB
FileController.class 4KB
FileUploadController.class 2KB
FileUploadController.class 2KB
FileUploadController$1.class 2KB
FileUploadController$1.class 2KB
FileController$1.class 2KB
FileController$1.class 2KB
FileInfo.class 2KB
FileInfo.class 2KB
FileController$2.class 2KB
FileController$2.class 2KB
FileStringUtil.class 724B
FileStringUtil.class 724B
webuploader.css 515B
webuploader.css 515B
webuploader.css 515B
file-service-demo.iml 4KB
file-client-demo.iml 4KB
jackson-databind-2.8.3.jar 1.18MB
jackson-databind-2.8.3.jar 1.18MB
spring-context-4.3.11.RELEASE.jar 1.09MB
spring-context-4.3.3.RELEASE.jar 1.08MB
spring-core-4.3.11.RELEASE.jar 1.07MB
spring-core-4.3.3.RELEASE.jar 1.06MB
spring-webmvc-4.3.3.RELEASE.jar 892KB
spring-webmvc-4.3.3.RELEASE.jar 892KB
spring-web-4.3.3.RELEASE.jar 794KB
spring-web-4.3.3.RELEASE.jar 794KB
spring-beans-4.3.11.RELEASE.jar 745KB
spring-beans-4.3.3.RELEASE.jar 743KB
spring-test-4.3.11.RELEASE.jar 586KB
spring-test-4.3.3.RELEASE.jar 579KB
log4j-1.2.17.jar 478KB
logback-core-1.2.2.jar 462KB
okhttp-3.9.1.jar 397KB
okhttp-3.9.1.jar 397KB
spring-aop-4.3.11.RELEASE.jar 372KB
spring-aop-4.3.3.RELEASE.jar 371KB
commons-lang3-3.1.jar 308KB
junit-4.12.jar 308KB
dom4j-1.6.1.jar 307KB
logback-classic-1.2.2.jar 284KB
jackson-core-2.8.3.jar 274KB
jackson-core-2.8.3.jar 274KB
spring-expression-4.3.3.RELEASE.jar 258KB
spring-expression-4.3.11.RELEASE.jar 257KB
commons-io-2.2.jar 170KB
commons-io-2.2.jar 170KB
xml-apis-1.0.b2.jar 107KB
okio-1.13.0.jar 80KB
okio-1.13.0.jar 80KB
commons-fileupload-1.3.1.jar 67KB
commons-fileupload-1.3.1.jar 67KB
commons-logging-1.2.jar 60KB
commons-logging-1.2.jar 60KB
jackson-annotations-2.8.0.jar 54KB
jackson-annotations-2.8.0.jar 54KB
hamcrest-core-1.3.jar 44KB
slf4j-api-1.7.25.jar 40KB
slf4j-api-1.7.2.jar 25KB
slf4j-log4j12-1.7.2.jar 9KB
FileController.java 7KB
FileUploadController.java 4KB
FileController.java 3KB
FileUploadController.java 2KB
FileInfo.java 2KB
FileStringUtil.java 656B
jquery-1.9.1.js 262KB
jquery-1.9.1.js 262KB
jquery-1.9.1.js 262KB
webuploader.min.js 70KB
webuploader.min.js 70KB
webuploader.min.js 70KB
index.jsp 4KB
index.jsp 4KB
index.jsp 4KB
index.jsp 346B
MANIFEST.MF 98B
MANIFEST.MF 98B
log4j.properties 764B
log4j.properties 764B
log4j.properties 764B
Uploader.swf 140KB
Uploader.swf 140KB
Uploader.swf 140KB
workspace.xml 102KB
workspace.xml 83KB
uiDesigner.xml 9KB
pom.xml 3KB
pom.xml 3KB
file_service_demo_war_exploded.xml 3KB
file_client_demo_war_exploded.xml 3KB
springmvc.xml 2KB
springmvc.xml 2KB
springmvc.xml 2KB
共 171 条
- 1
- 2
资源评论
- lichao1552500692019-07-05谢谢分享。非常感谢。。。。。。
- buqibao2019-04-13看看好不好用
不胖不换ID
- 粉丝: 27
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功