上传到项目下,路径写法:
// 获取根目录
File temp = null;
temp = new File(ResourceUtils.getURL("classpath:").getPath());
String path = temp.getAbsolutePath();
// 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
path = path.replace("\\target\\classes", "\\src\\main\\resources\\static\\upload");
<!--上传-->
<el-dialog title='上传文件' :visible.sync="importDialogVisible">
<p style="text-align: right"><el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button></p>
<h2 style="background-color: antiquewhite;text-align: left;padding: 5px">信息</h2>
<el-row :gutter="20">
<el-upload
class="upload-demo"
ref="upload"
:action="upload_method"
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="false">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</el-row>
</el-dialog>
//变量
importDialogVisible:false,
fileList: [],
upload_method:this.HOST + '/upload1'
//方法
submitUpload() {
this.$refs.upload.submit();
},
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
importInfo(row){
this.importDialogVisible = true;
console.log(row)
}
第一种:上传普通文件(如图片,表格等)
@RequestMapping("/upload1")
@ResponseBody
public void Upload1(@RequestParam MultipartFile file, HttpServletRequest request)throws IOException {
InputStream in = file.getInputStream();
String packageName = file.getOriginalFilename();
File tempFile = saveTempFile(in, packageName,request);//将上传的文件保存到本地