码迷,mamicode.com
首页 > Web开发 > 详细

上传下载文件

时间:2019-09-28 12:25:42      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:excel   word   super   jar包下载   mem   cti   protected   power   write   


技术图片

 

 

 


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设置参考上传的设置即可

 参考下载文件类型设置:

esponse.setContentType()的String参数及对应类型

<option   value="image/bmp">BMP</option>   
<option   value="image/gif">GIF</option>   
<option   value="image/jpeg">JPEG</option>   
<option   value="image/tiff">TIFF</option>   
<option   value="image/x-dcx">DCX</option>   
<option   value="image/x-pcx">PCX</option>   
<option   value="text/html">HTML</option>   
<option   value="text/plain">TXT</option>   
<option   value="text/xml">XML</option>   
<option   value="application/afp">AFP</option>   
<option   value="application/pdf">PDF</option>   
<option   value="application/rtf">RTF</option>   
<option   value="application/msword">MSWORD</option>   
<option   value="application/vnd.ms-excel">MSEXCEL</option>   
<option   value="application/vnd.ms-powerpoint">MSPOWERPOINT</option>   
<option   value="application/wordperfect5.1">WORDPERFECT</option>   
<option   value="application/vnd.lotus-wordpro">WORDPRO</option>   
<option   value="application/vnd.visio">VISIO</option>   
<option   value="application/vnd.framemaker">FRAMEMAKER</option>   
<option   value="application/vnd.lotus-1-2-3">LOTUS123</option>

上传下载文件

标签:excel   word   super   jar包下载   mem   cti   protected   power   write   

原文地址:https://www.cnblogs.com/tianshicomputer/p/11602435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!