package com.beyond.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import com.beyond.dao.FileBeanDao; import com.beyond.model.FileBean; import com.google.gson.Gson; @Controller @RequestMapping(value="file.do") public class FileController { @Resource(name="fileBeanDao") private FileBeanDao fileBeanDao; @RequestMapping(value="upload.do",method=RequestMethod.POST) public String onSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filename"); String name = multipartRequest.getParameter("filename"); System.out.println("name:"+name); String realFilename = file.getOriginalFilename(); System.out.println("获取文件名:"+realFilename); String ctxPath = request.getSession().getServletContext().getRealPath("/")+"download/"; System.out.println(ctxPath); File dirpath = new File(ctxPath); if(!dirpath.exists()){ dirpath.mkdir(); } String s = String.format("aaa %d", "a"); File uploadFile = new File(ctxPath+realFilename); FileCopyUtils.copy(file.getBytes(),uploadFile); request.setAttribute("fx", "上传成功"); return "views/upload"; } @RequestMapping(value="list.do",method=RequestMethod.POST) public @ResponseBody String loadFiles(HttpServletRequest request){ List<FileBean> files = new ArrayList<FileBean>(); String ctxpath = request.getSession().getServletContext().getRealPath("/")+"\\"+"download\\"; File file = new File(ctxpath); if(file.exists()){ File[] fs = file.listFiles(); for(File f : fs){ FileBean filebean = new FileBean(); filebean.setFilename(f.getName()); filebean.setSize(FormetFileSize(f.length())); if(f.isFile()){ files.add(filebean); } } } Gson gs = new Gson(); String listJson = gs.toJson(files); return listJson; } @RequestMapping(value="download.do") public void download(String fileName, HttpServletRequest request, HttpServletResponse response) { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); try { String path = request.getSession().getServletContext().getRealPath("/")+"download/"; InputStream inputStream = new FileInputStream(new File(path + File.separator + fileName)); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String FormetFileSize(long fileS) {// 转换文件大小 DecimalFormat df = new DecimalFormat("#"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "K"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "M"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; } @RequestMapping(value="onload.do",method=RequestMethod.POST) public String onload(HttpServletRequest request, HttpServletResponse response) throws Exception{ FileBean filebean = new FileBean(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filename"); String realFilename = file.getOriginalFilename(); System.out.println("获取文件名:"+realFilename); byte[] bb = file.getBytes(); System.out.println(); filebean.setContent(bb); filebean.setFilename(realFilename); filebean.setSize(FormetFileSize(bb.length)); fileBeanDao.insertEntity(filebean); FileBean fb = fileBeanDao.findEntityById(filebean.getId()); request.setAttribute("fb", fb); request.setAttribute("fx", "上传成功"); return "views/upload"; } @RequestMapping(value="listOne.do",method=RequestMethod.POST) public @ResponseBody String listFile(HttpServletRequest request) throws Exception{ FileBean fb = fileBeanDao.findEntityById(2); fb.setCon(new String(fb.getContent())); System.out.println(fb.getCon()); List<FileBean> list = new ArrayList<FileBean>(); list.add(fb); Gson gs = new Gson(); String listJson = gs.toJson(list); return listJson; } @RequestMapping(value="listall.do",method=RequestMethod.POST) public @ResponseBody String listallFile(HttpServletRequest request) throws Exception{ Map<String,Object> map = new HashMap<String,Object>(); List<FileBean> list = fileBeanDao.findEntity(map); for(FileBean f:list){ if(f.getFilename().endsWith(".txt")){ f.setCon(new String(f.getContent())); } } Gson gs = new Gson(); String listJson = gs.toJson(list); return listJson; } @RequestMapping(value="dlFile.do") public void dlFile(int id,String fileName, HttpServletRequest request, HttpServletResponse response) { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); try { FileBean filebean = fileBeanDao.findEntityById(id); OutputStream os = response.getOutputStream(); os.write(filebean.getContent()); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
最近下载更多
yymmdm LV6
2022年9月5日
matintalorr LV10
2022年3月25日
zhwang LV19
2021年5月19日
yaoweijie LV4
2021年4月17日
endual LV11
2021年2月16日
唯你不离不弃 LV5
2021年1月21日
hubugai1 LV11
2021年1月20日
zhoulang123 LV8
2020年10月16日
xuexizhuanyong23 LV16
2020年8月23日
912299793 LV21
2020年8月2日
最近浏览更多
Rucoding LV7
8月31日
dapeng0011 LV15
7月16日
潘潘123456 LV2
2023年12月24日
微信网友_6776384712986624
2023年12月13日
暂无贡献等级
uni-code_0123 LV1
2023年11月29日
lcqlcl LV11
2023年10月31日
张朕朕 LV3
2023年9月1日
李亮 LV19
2023年3月7日
yiposhop LV4
2023年2月9日
oneaaaaa
2022年11月21日
暂无贡献等级