码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC文件上传下载

时间:2019-12-08 01:30:58      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:random   mda   only   ann   write   代码   ctf   err   style   

jsp文件内容

文件上传

 <form id="addproductform" enctype="multipart/form-data" >

<td align="right">上传附件: </td>
<td>
<input type="file" onchange="upload()" name="download" ></input>
</td>
</tr>
<tr>
<td align="right">文件路径: </td>
<td>
<input name="filePath" id="filePath" type="text" readonly="readonly"></input>
</td>
<td align="right">上传附件: </td>
<td>
<input type="text" name="myfiles" id="myfiles" readonly="readonly"></input>
</td>
</tr>

</form>

 

function upload(){

//var myfiles =$(‘#myfiles‘).val();
var fomdata=new FormData($(‘#addproductform‘)[0])
//alert(‘进来看看fomdata==‘+fomdata)

$.ajax({
url:‘<%=basePath%>file/fileUpload.do‘,//地址
data:fomdata,
type:‘post‘,//类型
cache:false,
processData:false,
contentType:false,
//请求成功
success:function(data){
//alert(data.fileName);
var filePath=data.filePath;
$("#filePath").val(filePath);
$("#myfiles").val(data.fileName);

},
//失败/超时
error:function(XMLHttpRequest,textStatus,errorThrown){
if(textStatus===‘timeout‘){
alert(‘請求超時‘);
setTimeout(function(){
alert(‘重新请求‘);
},2000);
}
//alert(errorThrown);
}
})

文件下载

<form id="updateproductform">

<tr>

<td align="right">上传附件: </td>
<td>
<input type="text" name="myfiles" id="myfilesdown" readonly="readonly"></input>
</td>
</tr>
<tr>
<td align="right">文件路径: </td>
<td>
<input name="filePath" id="filePathdown" type="text" readonly="readonly"></input>
</td>
<td align="right">下载: </td>
<td>
<input value="下载" type="button" onclick="download()"></input>
</td>
</tr>

function download(){
var fileName=$(‘#myfilesdown‘).val();
var filePathdown=$(‘#filePathdown‘).val();
if(null!=fileName&&""!=fileName){
window.location.href="<%=basePath%>file/download.do?fileName="+fileName;
}
}

后台代码==

package controller.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/file")
public class FileUploadController {
//文件上传
@RequestMapping("/fileUpload.do")
@ResponseBody
public Map fileUpload(@RequestParam("download") MultipartFile file, HttpServletRequest request) {
System.out.println("file====================="+file);
Map<String,Object>map=new HashMap<String,Object>();
try {
if (!file.isEmpty()) {
String storePath = "D://images";
Random r = new Random();
String fileName = file.getOriginalFilename();
String[] split = fileName.split(".jpg");
fileName = split[0] + r.nextInt(1000);
fileName = fileName + ".jpg";
File filePath = new File(storePath, fileName);
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();// 如果目录不存在,则创建目录
}
file.transferTo(new File(storePath + File.separator + fileName));// 把文件写入目标文件地址
System.out.println("filePath+fileName="+filePath+fileName);
map.put("msg", "上传成功");
map.put("success", "success");
map.put("filePath", storePath);
map.put("fileName", fileName);
}


} catch (Exception e) {
e.printStackTrace();
map.put("msg", "上传失败");
}
return map;

}


//文件下载
@RequestMapping("/download.do")
public void download(@RequestParam("fileName")String filename,HttpServletRequest req,HttpServletResponse resp) throws IOException{
System.out.println("filename=="+filename);
//设置响应流文件进行下载
resp.setHeader("Content-Disposition","attachment;filename="+filename);
ServletOutputStream sos = resp.getOutputStream();
File file = new File("D://images", filename);//这个路径为磁盘开始

byte[] bytes = FileUtils.readFileToByteArray(file);
sos.write(bytes);
sos.flush();
sos.close();
}


}

springMVC文件上传下载

标签:random   mda   only   ann   write   代码   ctf   err   style   

原文地址:https://www.cnblogs.com/xianz666/p/12004075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!