标签:
java文件上传与下载根据文件起止位置,读取上传文件内容,保存到本地
Jsp页面
<form action="uploadServlet.do" method="post" enctype="multipart/form-data"> <input id="myfile" name="myfile" type="file" /> <input type="submit" value="提交" />${result} </form> 下载: <a href="downloadServlet.do?filename=test1.txt">test1.txt</a> ${errorResult}
public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从request当中获取流信息 InputStream fileSource = req.getInputStream(); String tempFileName = "E:/tempFile"; //tempFile指向临时文件 File tempFile = new File(tempFileName); //outputStram文件输出流指向这个临时文件 FileOutputStream outputStream = new FileOutputStream(tempFile); byte b[] = new byte[1024]; int n; while(( n = fileSource.read(b)) != -1){ outputStream.write(b, 0, n); } //关闭输出流、输入流 outputStream.close(); fileSource.close(); //获取上传文件的名称 RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r"); // l = new String(l.getBytes("8859_1"),"gbk"); String str2 = randomFile.readLine(); //编码转换 str2 = new String(str2.getBytes("8859_1"),"utf-8"); System.out.println(str2); String str = randomFile.readLine(); str = new String(str.getBytes("8859_1"),"utf-8"); System.out.println(str); int beginIndex = str.lastIndexOf("=") + 2; int endIndex = str.lastIndexOf("\""); String filename = str.substring(beginIndex, endIndex); System.out.println("filename:" + filename); //重新定位文件指针到文件头 randomFile.seek(0); long startPosition = 0; int i = 1; //获取文件内容 开始位置 while(( n = randomFile.readByte()) != -1 && i <=4){ if(n == '\n'){ startPosition = randomFile.getFilePointer(); i ++; } } startPosition = randomFile.getFilePointer() -1; //获取文件内容 结束位置 randomFile.seek(randomFile.length()); long endPosition = randomFile.getFilePointer(); int j = 1; while(endPosition >=0 && j<=2){ endPosition--; randomFile.seek(endPosition); if(randomFile.readByte() == '\n'){ j++; } } endPosition = endPosition -1; //设置保存上传文件的路径 //路径可以自行设置 String realPath = "E:\\myeclipse workplace\\css+js"; // String realPath = getServletContext().getRealPath("/") + "images"; File fileupload = new File(realPath); System.out.println(realPath); if(!fileupload.exists()){ fileupload.mkdir(); } File saveFile = new File(realPath,filename); RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw"); //从临时文件当中读取文件内容(根据起止位置获取) randomFile.seek(startPosition); while(startPosition < endPosition){ randomAccessFile.write(randomFile.readByte()); startPosition = randomFile.getFilePointer(); } //关闭输入输出流、删除临时文件 randomAccessFile.close(); randomFile.close(); tempFile.delete(); req.setAttribute("result", "上传成功!"); RequestDispatcher dispatcher = req.getRequestDispatcher("test.jsp"); dispatcher.forward(req, resp); } }
public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // req.setCharacterEncoding("utf-8"); // resp.setCharacterEncoding("utf-8"); // resp.setContentType("text/html;charset=utf-8"); // req.setCharacterEncoding("utf-8"); // resp.setContentType("text/html;charset=UTF-8"); // resp.setContentType("text/plain"); //进行编码的转换,因为不能识别中文 resp.setHeader("content-type","text/html;charset=UTF-8"); String path = getServletContext().getRealPath("/") + "images/"; String fileName = req.getParameter("filename"); String filename = null; filename = new String(fileName.getBytes("8859_1"),"utf-8"); // filename = new String(filename.getBytes("8859_1"),"uft-8"); System.out.println("路径:" + path + "文件名:" + filename); File file = new File(path + filename); if (file.exists()) { //由于下载的时候与浏览器的编码不符,浏览器不能识别中文编码,这里要进行转换 String value = new String(filename.getBytes("utf-8"),"ISO-8859-1"); resp.setContentType("application/x-msdownload"); resp.setHeader("Content-Disposition", "attachment;filename=\"" + value + "\""); InputStream inputStream = new FileInputStream(file); ServletOutputStream outputStream = resp.getOutputStream(); byte b[] = new byte[1024]; int n; while ((n = inputStream.read(b)) != -1) { outputStream.write(b, 0, n); } outputStream.close(); inputStream.close(); } else { req.setAttribute("errorResult", "文件不存在下载失败!!"); resp.sendRedirect("luntan.jsp"); } } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
标签:
原文地址:http://blog.csdn.net/wojiaohuangyu/article/details/50946428