上传下载文件


上传下载文件

 

 

 


1.文件上传---JSP+Servlet

 

准备工作:

  • jspsamrt.jar包下载

       链接:https://pan.baidu.com/s/1p56ZLlGvAmnDfVegjJ86TQ  提取码:u8ig 

  • 然后将该包复制到WEB-INF的lib路径下。

uploadForm.jsp  下面的页面字符集为gb2312,为了解决乱码问题

<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="utf-8"%>
<html>
<body>
<center>文件上传</center>
<hr>
<form action="UploadServlet" method="POST" enctype="multipart/form-data">
请选择一个文件上传
<input type="file" name="myFile">
<input type="submit" name="上传">
</form>
<br>${msg}
</body>
</html>


 

上传文件的Servlet--------UploadServlet.java

package com.tianshi.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//引包
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.jspsmart.upload.File;
/**
* Servlet implementation class UploadServlet
*/
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
SmartUpload smartUpload=new SmartUpload();
//初始化
ServletConfig config=this.getServletConfig();
smartUpload.initialize(config, request, response);
try {
//上传文件
smartUpload.upload();
//收到上传的文件对象
File smartFile=smartUpload.getFiles().getFile(0);
//保存文件
smartFile.saveAs("d:/"+smartFile.getFileName(),smartUpload.SAVE_PHYSICAL);
System.out.print("d:/"+smartFile.getFileName());
}catch(SmartUploadException e) {
e.printStackTrace();
}
String msg="upload success!";
System.out.print("ok");
request.setAttribute("msg", msg);
RequestDispatcher rd=request.getRequestDispatcher("uploadForm.jsp");
rd.forward(request, response);
}

}

 


web.xml配置

<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.tianshi.servlets.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>

 

 

 


2.文件下载jsp+servlet

准备工作:

  • 在WebContent下保存aa.txt文件

download.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<body>
文件下载
<br>
<hr>
<br>
<a href="downLoad?file=aa.txt">download</a>
</body>
</html>


DownLoad.java

package com.tianshi.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class DownLoad
*/
public class DownLoad extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public DownLoad() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String filename=request.getParameter("file");
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "attachment;filename="+filename);
try{RequestDispatcher rd=request.getRequestDispatcher("/"+filename);
if(rd!=null)
rd.forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}


 

web.xml设置参考上传的设置即可

 

上一篇:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path jsp开始


下一篇:org.hibernate.HibernateException: connnection proxy not usable after transaction