package mypack;
import general_class.Movie;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String filePath; //存放上传文件的目录
private String tempFilePath; //存放临时文件的目录
private Movie movie;//
public UploadServlet() {
super();
movie = new Movie();
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
filePath = config.getInitParameter("filePath");//读取初始化参数filePath
tempFilePath = config.getInitParameter("tempFilePath");//读取初始化参数tempFilePath
filePath= getServletContext().getRealPath(filePath);
tempFilePath = getServletContext().getRealPath(tempFilePath);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/plain;charset=GBK");
//定义向客户端发送响应正文的outNet
PrintWriter outNet = response.getWriter();
try{
//创建一个基于硬盘的FileItem工厂----------------(1)
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置向硬盘写数据时所用缓冲区的大小,此处为4K
factory.setSizeThreshold(4*1024);
//设置临时目录
factory.setRepository(new File(tempFilePath));
//设置一个文件上传处理器,与基于硬盘的ItemFactory对象关联-----------------(2)
ServletFileUpload upload = new ServletFileUpload(factory);
//设置允许上传的文件的最大尺寸,此处为2G
upload.setSizeMax(2*1024*1024*1024);
@SuppressWarnings("rawtypes")
List items = upload.parseRequest(request);//上传处理器解析HttpServletRequest对象
//中的表单数据--------------(3)
@SuppressWarnings("rawtypes")
Iterator iter = items.iterator();//*******
while(iter.hasNext()){
FileItem item = (FileItem)iter.next();
if(item.isFormField()){
processFormField(item,outNet);//处理普通的表单域
}else{
processUploadedFile(item,outNet);//处理上传文件
}
}
/*与另一JSP在请求范围内协作的代码*/
request.setAttribute("movie",movie);
ServletContext context = getServletContext();
RequestDispatcher addtodbDisp = context.getRequestDispatcher("/addtodb.jsp");
//包含
addtodbDisp.include(request, response);
outNet.close();
}catch(Exception e){
throw new ServletException(e);
}
}
private void processFormField(FileItem item,PrintWriter outNet) throws UnsupportedEncodingException{
String name = item.getFieldName();
String value= item.getString();
if(value!=null)
value = new String(value.getBytes("ISO-8859-1"),"gbk");
outNet.println(name+":"+value+"\r\n");
///
if(name.equals("moviename"))movie.setName(value);
else if(name.equals("category"))movie.setCategory(value);
else if(name.equals("actors")){movie.setActors(value);}
else if(name.equals("remark")){movie.setRemark(value);}
}
private void processUploadedFile(FileItem item,PrintWriter outNet)throws Exception{
String filename = item.getName();//获得文件名字
long fileSize = item.getSize(); //获得文件大小
if(filename.equals("" )&& fileSize==0)return;
int index = filename.lastIndexOf(".");
String format = filename.substring(index+1,filename.length());//获得文件格式
System.out.println("文件格式为:"+format);
/*根据文件的格式确定不同的存储路径,并将路径保存到实例变量中*/
if(format.equals("jpg")||format.equals("bmp")){
filePath = "E:\\film\\post";
String url = new String(filePath+"\\"+filename);
url = url.replaceAll("\\\\", "\\\\\\\\");System.out.println("海报存储到了:"+url);
movie.setPost_path(url);
}else{
filePath = "E:\\film\\store";
String url = new String(filePath+"\\"+filename);
url = url.replaceAll("\\\\","\\\\\\\\");System.out.println("影片存储到了:"+url);
movie.setMovie_url(url);
}
File uploadedFile = new File(filePath+"/"+filename);
item.write(uploadedFile);//把文件保存到本地硬盘
outNet.println(filename+" is saved.");
outNet.println("The size of "+filename+" is "+fileSize+"字节\r\n");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于文件上传功能的媒体资源管理系统
共30个文件
class:7个
java:7个
jar:3个
4星 · 超过85%的资源 需积分: 15 15 下载量 27 浏览量
2012-07-28
09:53:21
上传
评论
收藏 974KB RAR 举报
温馨提示
用servlet+jsp 实现文件上传功能,并将上传的文件信息添加到myslq数据库中
资源推荐
资源详情
资源评论
收起资源包目录
web-exp.rar (30个子文件)
web-exp
.project 1KB
WebContent
WEB-INF
lib
commons-io-2.3.jar 178KB
mysql-connector-java-5.1.20-bin.jar 784KB
commons-fileupload-1.2.2.jar 58KB
web.xml 2KB
show_movies.jsp 1KB
META-INF
MANIFEST.MF 39B
addtodb.jsp 1KB
upload.html 1KB
src
general_class
Movie.java 258B
Film.java 979B
Tvplay.java 67B
Episode.java 70B
mypack
UploadServlet.java 5KB
ImageProcess.java 1KB
ImageToStream.java 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 473B
.jsdtscope 503B
build
classes
general_class
Film.class 1KB
Movie.class 544B
Tvplay.class 273B
Episode.class 278B
mypack
UploadServlet.class 6KB
ImageToStream.class 1KB
ImageProcess.class 1KB
.classpath 841B
共 30 条
- 1
资源评论
- yuemoshitu2014-04-23还行吧 就是感觉东西少点 应加强一下
- yc93122015-01-22打开稍微看了一下,感觉没实现什么东西
Hello_world_ssh
- 粉丝: 3
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功