小程序第四弹 文件上传下载
2017-08-30 本文已影响0人
玲珑花
页面效果,现在不写样式了,只写Js文件了
其中有
如何列表渲染页面
如何下载文件
如何上传文件
页面的代码地址:https://pan.baidu.com/s/1nuLdKoT
后台代码简单展示
文件上传
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
@SuppressWarnings("serial")
public class Uploadfile extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileItem file = null;
InputStream in = null;
request.setCharacterEncoding("UTF-8");
try {
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
// 1. 得到 FileItem 的集合 items
List<FileItem> items = upload.parseRequest(request);
// 2. 遍历 items:
for (FileItem item : items) {
String name = item.getFieldName();
// 若是一个一般的表单域, 打印信息
if (item.isFormField()) {
System.out.println("表单域,上传文件时的其余参数,在这里获得" + name);
} else {
System.out.println("文件" + item);
file = item;
}
}
// 上传文件
byte[] buff = new byte[1024];
if (file == null) {
} else {
// 写入文件到服务器
FileOutputStream out = new FileOutputStream("c:\\" + file.getName());
int rc = 0;
while ((rc = in.read(buff)) > 0) {
out.write(buff, 0, rc);
}
out.flush();
out.close();
}
response.setCharacterEncoding("utf-8");
response.getWriter().print("{\"code\":\"0\",\"message\":\"success\"}");
response.getWriter().flush();
response.getWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件下载
import java.io.File;
import java.io.FileInputStream;
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;
public class Download 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");
// 查询能下载的文件 也就是file文件夹里面的内容
String path = request.getSession().getServletContext().getRealPath("");
String pathurl = path + "\\file\\" + filename;
// System.out.println(pathurl);
// 告诉浏览器是以下载的方法获取到资源
// 告诉浏览器以此种编码来解析URLEncoder.encode(filename, "utf-8"))
// response.setContentType("text/html; charset=GBK");
// response.setHeader("content-disposition", "attachment; filename=" +
// URLEncoder.encode(filename, "utf-8"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gbk"), "iso-8859-1"));
// 获取到所下载的资源
FileInputStream fis = new FileInputStream(pathurl);
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
response.getOutputStream().write(buf, 0, len);
}
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
我在文件下载测试时,遇到问题,下载的文件,不管是临时保存或者永久保存都是没有后缀的文件,没有后缀就意味着无法打开,没有解决这个问题
只有这样写的情况,下载的文件才能有后缀名字
Paste_Image.png在电脑端测试时,下载的文件一般位于
C:\Users\administrator\AppData\Local\微信web开发者工具\User Data\WeappFileCache