package com.jy.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; 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 javax.servlet.http.HttpSession; import net.sf.json.JSONArray; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import com.jy.common.ExportExcel; import com.jy.common.FileCommon; import com.jy.common.SessionInfo; import com.jy.dao.Json; import com.jy.excel.bean.BugTest; import com.jy.model.Bug; import com.jy.model.Bug_level; import com.jy.model.Jy_Bug_files; import com.jy.model.Project; import com.jy.model.ProjectFile; import com.jy.model.Test; import com.jy.model.User; import com.jy.service.BugService; /** * zhaokejin@outlook.com * * @author zkj * */ @Controller @RequestMapping(value = "/bug.do") public class BugListController { //文件下载 @RequestMapping(params = "method=downloadfile") public String downloadfile(String file_url,String storeName,HttpServletResponse response,HttpServletRequest request){ System.out.println(file_url); try { request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; File file=new File(file_url); if(file.exists()){ long fileLength = new File(file_url).length(); if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { storeName = URLEncoder.encode(storeName, "UTF-8"); } else { storeName = new String(storeName.getBytes("UTF-8"), "ISO8859-1"); } response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment; filename=" + storeName); //设置输出长度 response.setHeader("Content-Length", String.valueOf(fileLength)); //获取输入流 bis = new BufferedInputStream(new FileInputStream(file_url)); //输出流 bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } //关闭流 bis.close(); bos.close(); }else{ request.setAttribute("downloaderror", "1"); return "bug/editbuglist"; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @RequestMapping(params = "method=uploadFile") public String upload(HttpServletResponse response, HttpServletRequest request, int bug_id, HttpSession session) throws IOException { System.out.println(bug_id+"========================"); User user = (User) session.getAttribute(SessionInfo.SessionName); String responseStr = ""; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); // 文件保存路径 ctxPath本地路径 String ctxPath = request.getSession().getServletContext() .getRealPath("/") + "uploadFiles"; System.out.println(ctxPath); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); String ymd = sdf.format(new Date()); ctxPath += File.separator + ymd + File.separator; System.out.println("ctxpath=" + ctxPath); // 创建文件夹 File file = new File(ctxPath); if (!file.exists()) { file.mkdirs(); } // String fileName = null; Jy_Bug_files jy_Bug_files = null; for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { // // 上传文件 MultipartFile mf = entity.getValue(); // fileName = mf.getOriginalFilename();//获取原文件名 // System.out.println("filename="+fileName); // File uploadFile = new File(ctxPath + fileName); // try { // for(int i = 0;i<file.length;i++){ // MultipartFile my = file[i]; String filename = mf.getOriginalFilename(); System.out.println("filename=" + filename); if (filename != null && !"".equals(filename)) { String prefix = filename.substring(filename.lastIndexOf(".")); try { File newfile = file .createTempFile("bugFiles", prefix, file); mf.transferTo(newfile); jy_Bug_files = new Jy_Bug_files(); // 放入数据库 // jy_Bug_files.setBug_file_name(mf.getOriginalFilename()); // jy_Bug_files.setBug_file_size(FileCommon.FormetFileSize(mf // .getSize())); // jy_Bug_files.setBug_file_person(user.getUsername()); // jy_Bug_files.setBug_file_url(newfile.getAbsolutePath()); // jy_Bug_files.setBug_id(bug_id); // bugService.uploadfile(jy_Bug_files); // FileCopyUtils.copy(mf.getBytes(), uploadFile); responseStr = "上传成功"; } catch (IOException e) { responseStr = "上传失败"; e.printStackTrace(); } } response.setHeader("Content-type", "text/html;charset=UTF-8"); // servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding("UTF-8"); response.getWriter().write(responseStr); } return null; } }