java时间文件的上传
2018-02-01 20:17
113 查看
上传对表单的限制:
*method="post"--上传的内容显示在页面不好
*enctype="multipart/form-data"
*<input type="file" name="xxx"/>
jsp页面代码:
<form action="/Demo_file/Up3" method="post" enctype="multipart/form-data">
<input type="file" name="zhaopian">
<input type="submit" value="上传">
</form>Servlet代码:
package com.sxjyatc.Up;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 演示上传文件的文件名路径限制
* @author Administrator
*
*/
public class UploadServlet3 extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*编码问题*/
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//创建工厂
DiskFileItemFactory factory1 = new DiskFileItemFactory();
//创建工厂解析器
ServletFileUpload fileU = new ServletFileUpload(factory1);
//限制单个文件大小为100k
fileU.setFileSizeMax(100*1024);
//限制整个文件大小
fileU.setSizeMax(1024*1024);
try {
//得到fileItem集合
List<FileItem> fileItem = fileU.parseRequest(req);
//得到文件表单
FileItem file1 = fileItem.get(0);
/*
*1,得到文件的保存路径
*/
String root = this.getServletContext().getRealPath("/WEB-INF/file1");
//先去掉绝对路径
String filename = file1.getName();
int x = filename.lastIndexOf("\\");
if(x!=-1){
filename=filename.substring(x+1);
}
/*
* 文件同名问题
* 用uuid 生成不重复的随机码
*/
String savename = UUID.randomUUID().toString().replace("-", "")+"_"+filename;
/*得到文件名的hashcode*/
int filehash = filename.hashCode();
//1,将hashcode转成16位
String hex=Integer.toHexString(filehash);
//2,获取hex前两个字母 与文件名合在一起
File driFile = new File(root,hex.charAt(0)+"/"+hex.charAt(1));
//3,生成目录连
driFile.mkdirs();
//4,创建目录文件
File dostFile = new File(driFile,savename);
//写入文件
try {
file1.write(dostFile);
System.out.println("完成");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}要用到的两个包:
commons-fileupload-1.3.3.jar(基于io包才可以用)
commons-io-2.6.jar
*method="post"--上传的内容显示在页面不好
*enctype="multipart/form-data"
*<input type="file" name="xxx"/>
jsp页面代码:
<form action="/Demo_file/Up3" method="post" enctype="multipart/form-data">
<input type="file" name="zhaopian">
<input type="submit" value="上传">
</form>Servlet代码:
package com.sxjyatc.Up;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 演示上传文件的文件名路径限制
* @author Administrator
*
*/
public class UploadServlet3 extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*编码问题*/
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//创建工厂
DiskFileItemFactory factory1 = new DiskFileItemFactory();
//创建工厂解析器
ServletFileUpload fileU = new ServletFileUpload(factory1);
//限制单个文件大小为100k
fileU.setFileSizeMax(100*1024);
//限制整个文件大小
fileU.setSizeMax(1024*1024);
try {
//得到fileItem集合
List<FileItem> fileItem = fileU.parseRequest(req);
//得到文件表单
FileItem file1 = fileItem.get(0);
/*
*1,得到文件的保存路径
*/
String root = this.getServletContext().getRealPath("/WEB-INF/file1");
//先去掉绝对路径
String filename = file1.getName();
int x = filename.lastIndexOf("\\");
if(x!=-1){
filename=filename.substring(x+1);
}
/*
* 文件同名问题
* 用uuid 生成不重复的随机码
*/
String savename = UUID.randomUUID().toString().replace("-", "")+"_"+filename;
/*得到文件名的hashcode*/
int filehash = filename.hashCode();
//1,将hashcode转成16位
String hex=Integer.toHexString(filehash);
//2,获取hex前两个字母 与文件名合在一起
File driFile = new File(root,hex.charAt(0)+"/"+hex.charAt(1));
//3,生成目录连
driFile.mkdirs();
//4,创建目录文件
File dostFile = new File(driFile,savename);
//写入文件
try {
file1.write(dostFile);
System.out.println("完成");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}要用到的两个包:
commons-fileupload-1.3.3.jar(基于io包才可以用)
commons-io-2.6.jar
相关文章推荐
- CKFinder(Java版)以时间作上传文件的保存路径
- 彻底搞定java文件上传(转)
- java文件上传原理
- 自己写的一个JSP上传文件和下载文件的JavaBean
- 一个JSP上传文件和下载文件的JavaBean
- VB.NET客户端调用Axis的Java Web Service实现文件上传
- java做的比较完善的FTP连接上传下载文件
- java文件上传和下载(原创,根据RFC1867分析得到)
- 带进度条的文件上传(java+ajax,附源码)
- java上传多个文件
- java上传文件
- java文件上传下载
- 彻底搞定java文件上传
- 带进度条的文件上传(java+ajax,附源码
- [ZT]彻底搞定java文件上传zz
- [转载] java文件上传原理
- 用java做c/s架构的文件上传功能
- 彻底搞定java文件上传
- java文件上传资料 ctrl C + ctrl V again sorry everybody~
- 彻底搞定java文件上传