jsp文件上传下载系统|JSP做的简单的文件上传下载代码

jsp文件上传下载系统|JSP做的简单的文件上传下载代码的第1张示图

⑴ jsp上传下载文件的路径问题

jsp上传下载文件的路径是在服务器建立指定路径如下://接收上传文件内容中临时文件的文件名 String tempFileName = new String("tempFileName"); //tempfile 对象指向临时文件 File tempFile = new File("D:/"+tempFileName); //outputfile 文件输出流指向这个临时文件 FileOutputStream outputStream = new FileOutputStream(tempFile); //得到客服端提交的所有数据 InputStream fileSourcel = request.getInputStream(); //将得到的客服端数据写入临时文件 byte b[] = new byte[1000]; int n ; while ((n=fileSourcel.read(b))!=-1){ outputStream.write(b,0,n); } //关闭输出流和输入流 outputStream.close(); fileSourcel.close(); //randomFile对象指向临时文件 RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r"); //读取临时文件的第一行数据 randomFile.readLine(); //读取临时文件的第二行数据,这行数据中包含了文件的路径和文件名 String filePath = randomFile.readLine(); //得到文件名 int position = filePath.lastIndexOf('\\'); CodeToString codeToString = new CodeToString(); String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1)); //重新定位读取文件指针到文件头 randomFile.seek(0); //得到第四行回车符的位置,这是上传文件数据的开始位置 long forthEnterPosition = 0; int forth = 1; while((n=randomFile.readByte())!=-1&&(forth<=4)){ if(n=='\n'){ forthEnterPosition = randomFile.getFilePointer(); forth++; } } //生成上传文件的目录 File fileupLoad = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file","upLoad"); fileupLoad.mkdir(); //saveFile 对象指向要保存的文件 File saveFile = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename); RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw"); //找到上传文件数据的结束位置,即倒数第四行 randomFile.seek(randomFile.length()); long endPosition = randomFile.getFilePointer(); int j = 1; while((endPosition>=0)&&(j<=4)){ endPosition–; randomFile.seek(endPosition); if(randomFile.readByte()=='\n'){ j++; } } //从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中 randomFile.seek(forthEnterPosition); long startPoint = randomFile.getFilePointer(); while(startPoint<endPosition){ randomAccessFile.write(randomFile.readByte()); startPoint = randomFile.getFilePointer(); } //关闭文件输入、输出 randomAccessFile.close(); randomFile.close(); tempFile.delete();jsp文件下载选择路径://要下载的文件 File fileload = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename);

⑵ jsp+servlet实现文件上传与下载源码

上传:需要导入两个包:commons-fileupload-1.2.1.jar,commons-io-1.4.jarimport java.io.File;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;/** * 上传附件 * @author new * */public class UploadAnnexServlet extends HttpServlet { private static String path = ""; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /* * post处理 * (non-Javadoc) * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { path = this.getServletContext().getRealPath("/upload"); try { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload up = new ServletFileUpload(factory); List<FileItem> ls = up.parseRequest(request); for (FileItem fileItem : ls) { if (fileItem.isFormField()) { String FieldName = fileItem.getFieldName(); //getName()返回的是文件名字 普通域没有文件 返回NULL // String Name = fileItem.getName(); String Content = fileItem.getString("gbk"); request.setAttribute(FieldName, Content); } else { String nm = fileItem.getName().substring( fileItem.getName().lastIndexOf("\\") + 1); File mkr = new File(path, nm); if (mkr.createNewFile()) { fileItem.write(mkr);//非常方便的方法 } request.setAttribute("result", "上传文件成功!"); } } } catch (Exception e) { e.printStackTrace(); request.setAttribute("result", "上传失败,请查找原因,重新再试!"); } request.getRequestDispatcher("/pages/admin/annex-manager.jsp").forward( request, response); }} 下载(i/o流)无需导包:import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 下载文件 * @author * */public class DownloadFilesServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 8594448765428224944L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /* * 处理请求 * (non-Javadoc) * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("fileName"); System.out.print("dddddddddd:" + name); // web绝对路径 String path = request.getSession().getServletContext().getRealPath("/"); String savePath = path + "upload"; // 设置为下载application/x-download response.setContentType("application/x-download"); // 即将下载的文件在服务器上的绝对路径 String filenamedownload = savePath + "/" + name; // 下载文件时显示的文件保存名称 String filenamedisplay = name; // 中文编码转换 filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filenamedisplay); try { java.io.OutputStream os = response.getOutputStream(); java.io.FileInputStream fis = new java.io.FileInputStream( filenamedownload); byte[] b = new byte[1024]; int i = 0; while ((i = fis.read(b)) > 0) { os.write(b, 0, i); } fis.close(); os.flush(); os.close(); } catch (Exception e) { } }}

⑶ 请问谁会文件的上传和下载啊,基于jsp的,直接右击连接,另存为的那种

直接点击下载的只有RAR RMVB 这些可以 要是.doc,.txt就不行了 必须用文件读取来解决 JSP文件头也要改 例如response.setContentType("application/x-download");//设置为下载application/x-download response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0);

⑷ 求jsp用数据库上传下载文件的例子!

我有个例子,是将文件保存到服务器的目录中,并把文件的路径保存进数据库中,我给你发过去了,你可以看看,可能会对你有些帮助

⑸ jsp 如何实现文件上传和下载功能

上传:

MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub

FormFile fname=mf.getFname();

byte [] fn = fname.getFileData();

OutputStream out = new FileOutputStream("D:\"+fname.getFileName());

Date date = new Date();

String title = fname.getFileName();

String url = "d:\"+fname.getFileName();

Upload ul = new Upload();

ul.setDate(date);

ul.setTitle(title);

ul.setUrl(url);

UploadDAO uld = new UploadDAO();

uld.save(ul);

out.write(fn);

out.close();

下载:

DownloadForm downloadForm = (DownloadForm)form;

String fname = request.getParameter("furl");

FileInputStream fi = new FileInputStream(fname);

byte[] bt = new byte[fi.available()];

fi.read(bt);

//设置文件是下载还是打开以及打开的方式msdownload表示下载;设置字湖集,//主要是解决文件中的中文信息

response.setContentType("application/msdownload;charset=gbk");

//文件下载后的默认保存名及打开方式

String contentDisposition = "attachment; filename=" + "java.txt";

response.setHeader("Content-Disposition",contentDisposition);

//设置下载长度

response.setContentLength(bt.length);

ServletOutputStream sos = response.getOutputStream();

sos.write(bt);

return null;

⑹ jsp项目中涉及文件的上传和下载怎么实现

你是用什么框架,如果里面有struts的话可以用struts自带有上传下载的功能,只需要写代码就好了,如果需要加我987781070,如果没有用到struts可以用ajax实现,这个自己网络就好了,很多案例的!

⑺ jsp实现文件上传到mysql数据库并能下载文件模块案列

不知道你基础怎么样,这是最原始最简单的案例了,html代码是你要选择文件的界面,jsp代码请命名为smartupload_demo02.jsp(因为html里面表单的action是提交到这个页面,当然你可以改,html表单action与jsp名称一致就行)另外还有很重要的一件事,你需要下载一个SmartUpload.jar插件放到你的web目录的lib文件夹下面,如果你不知道怎么下网络,再不行给个邮箱我发给你。不理解的地方可以联系我[email protected]qq.com,希望可以帮到你。html代码:<html><head><title>www.mldnjava.cn,MLDN高端Java培训</title></head><body><form action="smartupload_demo02.jsp" method="post" enctype="multipart/form-data">姓名:<input type="text" name="uname"><br>照片:<input type="file" name="pic"><br><input type="submit" value="上传"><input type="reset" value="重置"></form></body></html>jsp代码:<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="org.lxh.smart.*"%><%@ page import="cn.mldn.lxh.util.*"%><html><head><title>www.mldnjava.cn,MLDN高端Java培训</title></head><body><%request.setCharacterEncoding("GBK") ;%><%SmartUpload smart = new SmartUpload() ;smart.initialize(pageContext) ;// 初始化上传操作smart.upload() ;// 上传准备String name = smart.getRequest().getParameter("uname") ;IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;// 取得客户端的IP地址String ext = smart.getFiles().getFile(0).getFileExt() ;// 扩展名称String fileName = its.getIPTimeRand() + "." + ext ;smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator + fileName) ;%><%=smart.getFiles().getFile(0).getFileName().matches("^\\w+.(jpg|gif)$")%><h2>姓名:<%=name%></h2><img src="../upload/<%=fileName%>"></body></html>

⑻ JSP做的简单的文件上传下载代码

晕,给你写完了,人怎么没了呢……

未经允许不得转载:山九号 » jsp文件上传下载系统|JSP做的简单的文件上传下载代码

赞 (0)