文件上传
2021/5/25 9:02:30
文件上传
程序调用接口上传文件
java语言
使用 HttpHelper.uploadFile 调用接口
HttpHelper
public static void testUpload() throws Exception {
String serverUrl = "http://localhost:8182//normalValue/fileImport";
String localFilePath = "e:/abc.png";
//附带其他信息
Map<String, String> map = new HashMap<>();
map.put("key", "value");
String result = HttpHelper.uploadFile(serverUrl, localFilePath, "file", map, null);
System.out.println(result);
Reulst theResult = JSON.parseObject(result, Reulst.class);
if (theResult.getStatus() != 0) {
throw new Exception("上传失败了");
}
}
Vue 前端调用接口上传文件
<form action="saveFile" enctype="multipart/form-data" method="post" id="fileUpload1">
<el-upload
class="upload-demo"
action="saveFile"
id="filename1"
name="file"
ref="upload"
:auto-upload="false"
:on-change="fileChange"
accept=".xlsx,.xls"
>
<span class="btn-line">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-upload" />
</svg>
<span>上传文件</span>
</span>
<span class="cg9">支持xls,xlsx格式文件</span>
</el-upload>
</form>
let form = document.getElementById('fileUpload1')
let postData = new FormData(form)
this.post('/normalValue/fileImport', postData, {})
fiddler 捕捉到得报文信息
POST http://localhost:8088/normalValue/fileImport HTTP/1.1
Host: localhost:8088
Connection: keep-alive
Content-Length: 169100
Accept: application/json, text/plain, */*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryYATxcz9sXQ9VnKfw
Origin: http://localhost:8088
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: showcode=; TABLE_SESSION=ecc438bb-c36b-46fa-a54d-59f5c6a2cbc0
------WebKitFormBoundaryYATxcz9sXQ9VnKfw
Content-Disposition: form-data; name="file"; filename="QQ图片20200827141856.jpg"
Content-Type: image/jpeg
JFIF C
等等信息
------WebKitFormBoundaryYATxcz9sXQ9VnKfw--
文件接收
java语言
@PostMapping("/normalValue/fileImport")
@ResponseBody
public BaseResponse<String> fileImportForClassStandard(HttpServletRequest request) {
BaseResponse<String> result = new BaseResponse<String>();
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
//获取附带其他信息
Map<String, String[]> map = multipartHttpServletRequest.getParameterMap();
MultipartFile file = multipartHttpServletRequest.getFile("file");
String count = "";
byte[] content = file.getBytes();
String fileName = file.getOriginalFilename();
String extName = fileName.substring(fileName.lastIndexOf("."));
if (!extName.equals(".xls") && !extName.equals(".xlsx")) {
result.setCode(500);
result.setMsg("请上传 xls 或 xlsx 类型的文件");
return result;
}
String filePath = getFilePath(file);
FileOutputStream outputStream = new FileOutputStream(filePath);
outputStream.write(content);
outputStream.close();
//然后对 filePath 得文件进行处理
扫码分享
版权说明
作者:SQBER
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
{0}
{5}
{1}
{2}回复
{4}
*昵称:
*邮箱:
个人站点:
*想说的话: