@Value("${DOWNLOAD_PATH}")
private String path;
@RequestMapping(value = "/uploadFile",method= RequestMethod.POST)
@ResponseBody
public void handleFormUpload(@RequestParam("file") MultipartFile file) {
AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
List<String> namePathlist=new ArrayList<String>();
try {
if (file != null && file.getOriginalFilename() != "") {
String realName = file.getOriginalFilename();
namePathlist.add(realName);
InputStream in = file.getInputStream();
String allPath=FileUtil.uploadFile(in, path,realName);
namePathlist.add(allPath);
}
} catch (Exception e) {
e.printStackTrace();
}
ajaxResponseBody.setSuccess(true);
ajaxResponseBody.setData(namePathlist);
ReturnUtil.success(request, response, ajaxResponseBody);
}
@RequestMapping(value = "/downloadFile")
@ResponseBody
public void adduser(HttpServletRequest request,HttpServletResponse response, String path,String realName) throws Exception {
FileUtil.downloadFile(path, realName, request, response);
}
package comframe.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUtil {
public static String uploadFile(InputStream is,String savePath,String realName) throws IOException{
File file = new File(savePath);
System.out.println(file.getCanonicalPath());
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath+"目录不存在,需要创建");
file.mkdir();
}
String filename = UUIDUtil.genStrByRandom(false);
String substring = realName.substring(realName.indexOf("."));
String allPath = savePath + "/" + filename+substring;
try {
FileOutputStream out = new FileOutputStream(allPath);
byte buffer[] = new byte[1024];
int len = 0;
while((len=is.read(buffer))>0){
out.write(buffer, 0, len);
}
is.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return allPath;
}
public static void downloadFile(String path,String realName,HttpServletRequest request, HttpServletResponse response) {
try {
String fileName = realName.toString();
File file = new File(path);
System.out.println(file.getCanonicalPath());
InputStream inStream = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[inStream.available()];
inStream.read(buffer);
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}