网站上传文件学习笔记

来源:互联网 发布:天猫优质男装品牌知乎 编辑:程序博客网 时间:2024/05/16 13:48

一、jsp页面的设计

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">  
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  <body>
  <form action="/upload/servlet/UpLoadServlet" method="post" name="upload" enctype="multipart/form-data">
  username<input type="text" name="username" /><br/>
  photo<input type="file" name="photo"/>
  <input type="submit"  name="photo" value="上传"/>
  </form>
  </body>
</html>

二、servlet编写

package com.guigu.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UpLoadServlet extends HttpServlet {
public UpLoadServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request.setCharacterEncoding("utf-8");
//文件的缓存工厂
String path=getServletContext().getRealPath("/WEB-INF/file");
String str=UUID.randomUUID().toString();
DiskFileItemFactory dff=new DiskFileItemFactory();
//设置临时文件
dff.setRepository(new File(path));
//开始创建request解析器
ServletFileUpload sfu=new ServletFileUpload(dff);
//设置文件大小上限,单位
sfu.setFileSizeMax(1024*1024*1024);
//通过解析器解析request
try {
List<FileItem> list= sfu.parseRequest(request);
FileItem f1=list.get(0);
FileItem f2=list.get(1);
InputStream is=f2.getInputStream();
File file=new File(path+"/"+str+".mp4");
FileOutputStream fos=new FileOutputStream(file);
byte[] b=new byte[1024];
int len=-1;
while((len=is.read(b))!=-1){
fos.write(b);
fos.flush();
}
fos.close();
is.close();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void init() throws ServletException {
}
}
0 0
原创粉丝点击