java+超大文件上传与下载
这里先说下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/
posted on 2019-08-05 13:58 Xproer-松鼠 阅读(3722) 评论(0) 编辑 收藏 举报