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

实现上传

时间:2017-06-02 09:57:27      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:als   file   session   ipa   odi   amp   ddn   get   eth   

method="post" enctype="multipart/form-data">

<%
request.setCharacterEncoding("UTF-8");

News news=new News();
news.setCreateDate(new Date());

boolean isMultipart=ServletFileUpload.isMultipartContent(request);
String realpath=request.getSession().getServletContext().getRealPath("upload/");
File getrealpath=new File(realpath);
if(!getrealpath.exists()){
getrealpath.mkdir();
}

if(isMultipart){
//创建工厂
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);

List<FileItem> list=upload.parseRequest(request);
Iterator<FileItem> iter=list.iterator();
while(iter.hasNext()){
FileItem fi=iter.next();
if(fi.isFormField()){
if(fi.getFieldName().equals("categoryId")){
news.setCategoryId(Integer.parseInt(fi.getString()));
}
if(fi.getFieldName().equals("title")){
news.setTitle(fi.getString("utf-8"));
}
if(fi.getFieldName().equals("newscontent")){
news.setContent(fi.getString("utf-8"));
}
if(fi.getFieldName().equals("author")){
news.setAuthor(fi.getString("utf-8"));
}
if(fi.getFieldName().equals("summary")){
news.setSummary(fi.getString("utf-8"));
}
}else{
String getname=fi.getName();
File file=new File(getname);
if(file.getName()!=null&&!file.getName().equals("")){
File realfile=new File(realpath,file.getName());
fi.write(realfile);
news.setPicPath(realfile.getName());
}

}


}

}








if(newsService.addNews(news)){
%>
<jsp:forward page="newsDetailList.jsp"/>
<%
//request.getRequestDispatcher("newsDetailList.jsp").forward(request, response);
}
%>

实现上传

标签:als   file   session   ipa   odi   amp   ddn   get   eth   

原文地址:http://www.cnblogs.com/m97i/p/6931568.html

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