-
Java中的文件上传下载——FileUploadAction.java public class FileUploadAction extends ActionSupport { private List<File> upload; private List<String> uploadContentType; private List<String> uploadFileName; private String result; setter/getter... @Override public String execute() throws Exception { String path = ServletActionContext.getServletContext().getRealPath("/images"); File file = new File(path); if(!file.exists()){ file.mkdir(); } //循环将批量上传的文件保存到本地 for(int i=0;i<upload.size();i++){ FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i))); } result="上传成功!"; return SUCCESS; } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——Struts2文件上传下载实现 II 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——Struts2文件上传下载实现 I 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——BatchDownloadServlet.java public class BatchDownloadServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/x-msdownload"); resp.setHeader("Content-Disposition", "attachment;filename=test.zip"); String path = getServletContext().getRealPath("/") + "images/"; String[] filenames = req.getParameterValues("filename"); String str = ""; String rt = "\r\n"; ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream()); for(String filename : filenames){ str += filename + rt; File file = new File(path + filename); zos.putNextEntry(new ZipEntry(filename)); FileInputStream fis = new FileInputStream(file); byte b[] = new byte[1024]; int n = 0; while((n = fis.read(b)) != -1){ zos.write(b, 0, n); } zos.flush(); fis.close(); } zos.setComment("download success:" + rt + str); zos.flush(); zos.close(); } }查看全部
-
Java中的文件上传下载——SmartDownloadServlet.java public class SmartDownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); SmartUpload su = new SmartUpload(); su.initialize(getServletConfig(), request, response); su.setContentDisposition(null); try { su.downloadFile("/images/"+ filename); } catch (SmartUploadException e) { e.printStackTrace(); } } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——使用SmartUpload实现文件下载 Ps:su.setContentDisposition(null); //设置下载后的打开方式 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——SmartUploadServlet.java II ... for(int i =0; i < su.getFiles().getCount(); i++){ com.jspsmart.upload.File tempFile = su.getFiles().getFile(i); System.out.println("---------------------------"); System.out.println("表单当中name属性值:" + tempFile.getFieldName()); System.out.println("上传文件名:" + tempFile.getFieldName()); System.out.println("上传文件长度:" + tempFile.getSize()); System.out.println("上传文件的拓展名:" + tempFile.getFileExt()); System.out.println("上传文件的全名:" + tempFile.getFilePathName()); System.out.println("---------------------------"); } req.setAttribute("result", result); req.getRequestDispatcher("jsp/02.jsp").forward(req, resp); } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——SmartUploadServlet.java I public class SmartUploadServlet 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 { //设置上传文件保存路径 String filePath = getServletContext().getRealPath("/") + "images"; File file = new File(filePath); if(!file.exists()){ file.mkdir(); } SmartUpload su = new SmartUpload(); //初始化对象 su.initialize(getServletConfig(), req, resp); //设置上传文件大小 su.setMaxFileSize(1024*1024*10); //设置所有文件的大小 su.setTotalMaxFileSize(1024*1024*100); //设置允许上传文件类型 su.setAllowedFilesList("txt,jpg,gif"); String result = "上传成功!"; //设置禁止上传的文件类型 try { su.setDeniedFilesList("rar,jsp,js"); //上传文件 su.upload(); int count = su.save(filePath); System.out.println("上传成功" + count + "个文件!"); } catch (Exception e) { result = "上传失败!"; e.printStackTrace(); }查看全部
-
Java中的文件上传下载——SmartUploadServlet上传异常:常见类型 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——使用SmartUpload实现文件批量上传 1、一般使用FileUpload / SmartUpload组件。 2、SmartUpload 由 www.jspsmart.com 网站开发的一套上传组件包,可以轻松的实现文件的上传下载功能。 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
设置文件下载查看全部
-
Smartupload中的下载,以附件方式下载查看全部
-
注意替换文件路径的写法 file:///查看全部
-
Java中的文件上传下载——UploadServlet.java III //设置保存上传文件的路径 String realPath = getServletContext().getRealPath("/") + "images"; File fileupload = new File(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("jsp/01.jsp"); dispatcher.forward(req, resp); } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
Java中的文件上传下载——UploadServlet.java II //重新定位文件指针到文件头 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; ... 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
举报
0/150
提交
取消