一、表单上传
html客户端部分:
<form action="upload.ashx" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file1" /><br />
<input type="submit" value="上传" />
</form>
程序后台服务端:
@ResponseBody
@RequestMapping(value = "/xxx",method = RequestMethod.POST)
public String xxx(@RequestParam("myfile") MultipartFile file)throws IOException{
//上传文件
String url = "D:/" + "upload/";
//后缀
String suffic = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fname =file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
String fileName = fname+System.currentTimeMillis()+suffic;
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
fileType = fileType.toLowerCase();
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(url, fileName));
return "";
}
二、ajax异步上传
html客户端部分:
<input id="resume_name" type="file" >
<input class="btn_success" type="button" id="upload" value="提交" />
<script src="jquery-2.1.4.js"></script>
<script>
$(function () {
$("#upload").click(function () {
$("#imgWait").show();
var formData = new FormData();
formData.append("myfile", document.getElementById("resume_name").files[0]);
$.ajax({
url: "upload.ashx",
type: "POST",
data: formData,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false,
success: function (data) {
if (data.status == "true") {
alert("上传成功!");
}
if (data.status == "error") {
alert(data.msg);
}
$("#imgWait").hide();
},
error: function () {
alert("上传失败!");
$("#imgWait").hide();
}
});
});
});
</script>
程序后台服务端:
<input id="resume_name" type="file" >
<input class="btn_success" type="button" id="upload" value="提交" />
<script src="jquery-2.1.4.js"></script>
<script>
$(function () {
$("#upload").click(function () {
$("#imgWait").show();
var formData = new FormData();
formData.append("myfile", document.getElementById("resume_name").files[0]);
$.ajax({
url: "upload.ashx",
type: "POST",
data: formData,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false,
success: function (data) {
if (data.status == "true") {
alert("上传成功!");
}
if (data.status == "error") {
alert(data.msg);
}
$("#imgWait").hide();
},
error: function () {
alert("上传失败!");
$("#imgWait").hide();
}
});
});
});
</script>
程序后台服务端:@ResponseBody
@RequestMapping(value = "/xxx",method = RequestMethod.POST)
public String xxx(@RequestParam("myfile") MultipartFile file)throws IOException{
//上传文件
String url = "D:/" + "upload/";
//后缀
String suffic = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fname =file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
String fileName = fname+System.currentTimeMillis()+suffic;
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
fileType = fileType.toLowerCase();
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(url, fileName));
return "";
}
@ResponseBody
@RequestMapping(value = "/xxx",method = RequestMethod.POST)
public String xxx(@RequestParam("myfile") MultipartFile file)throws IOException{
//上传文件
String url = "D:/" + "upload/";
//后缀
String suffic = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fname =file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
String fileName = fname+System.currentTimeMillis()+suffic;
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
fileType = fileType.toLowerCase();
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(url, fileName));
return "";
}