這里先說下spring mvc 遇到的坑,就是如果文件上傳時,后端這樣寫public String file1(HttpServletRequest request),根據request拿到的東西是空的。所以要下面這樣寫。
上傳
在任何xml里面(因為都要加載到的,所以可以隨便放進去)加上
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10485760" />
cotroller里面
@RequestMapping(value="file",method=RequestMethod.POST)
public String file1(@RequestParam("file")MultipartFile file1,HttpServletRequest request) throws IOException {
InputStream inputStream=file1.getInputStream();
//System.out.println(file1.getOriginalFilename()+" "+file1.getSize());
//第一種是原始的java文件上傳的方式,下載的話也跟這個差不多,所以下載就不寫了;
String name=request.getSession().getServletContext().getRealPath("/")+"file";
System.out.println(name);
File file0=new File(name);
if(!file0.isDirectory()&&!file0.exists())
file0.mkdir();
name+="\\"+file1.getOriginalFilename();
File file=new File(name);
try {
file.createNewFile();
FileOutputStream outputStream;
outputStream = new FileOutputStream(file);
byte b[]=new byte[1024];
int n;
while((n=inputStream.read(b))!=-1){
outputStream.write(b, 0, n);
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
//下面是xml引入的bean,也是比較方便的方法
/*
if(!file.isEmpty()){
try {
//這里將上傳得到的文件保存至 d:\\temp\\file 目錄
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("d:/hello",
System.currentTimeMillis()+ file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
}
*/
return "upload";
}
文件下載一般會涉及特定文件夾下的搜索文件(用迭代方法)
import java.io.File;
public class test {
public static void main(String[] args) {
String path="D:/hello/";
File file=new File(path);
researchfile(file);
}
public static void researchfile(File file) {
if (file.isDirectory()) {
File[] filearry = file.listFiles();
for (File f : filearry) {
if (f.isDirectory()) {
// System.out.println("0"+f.getAbsoluteFile());
} else {
System.out.println(f.getAbsoluteFile());
}
researchfile(f);
}
}
}
}
下載代碼
@RequestMapping("download")
public String downLoad(HttpServletResponse response) throws UnsupportedEncodingException {
String filename="報名照片.jpg";
String filePath = "D:/NewFile" ;
File file = new File(filePath + "/" + filename);
if(file.exists()){ //判斷文件父目錄是否存在
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + new String("報名照片.jpg".getBytes("gb2312"),"ISO_8859_1"));
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件輸入流
BufferedInputStream bis = null;
OutputStream os = null; //輸出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download" + filename);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
上面紅色代碼很重要,是讓系統返回的不是頁面,而是類似文件流的形式輸出。其次還
解決Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name) 中文顯示亂碼
詳細的配置信息可以參考這篇文章:http://blog.ncmem.com/wordpress/2019/08/07/java%E8%B6%85%E5%A4%A7%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8E%E4%B8%8B%E8%BD%BD/