代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
文件上传servlet处理代码
作者:
biergaizi2009
/ 发布于
2012/5/14
/
658
文件上传servlet处理代码
<div>package imageServlet;</div> import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger;</div> import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;</div> import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;</div> import util.MD5; import util.MyCalendar; import entity.User;</div> <div>/** * Servlet implementation class DoUploadNewsImageServlet */ public class DoUploadNewsImageServlet extends HttpServlet { private static final long serialVersionUID = 1L;</div> <div>/** * @see HttpServlet#HttpServlet() */ public DoUploadNewsImageServlet() { super(); // TODO Auto-generated constructor stub } <div>/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } <div>/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); User user = (User) request.getSession().getAttribute("user"); try { @SuppressWarnings("unchecked") List<FileItem> list = upload.parseRequest(request); for (FileItem fileItem : list) { int u_id = user.getId(); // 对name进行MD5加密 String name = MD5.getMD5((u_id + "_" + MyCalendar.getCalendar().getTimeInMillis()).getBytes()); File file = new File(getServletContext().getRealPath("/") + getServletContext().getInitParameter("newsImgFolder") + "/" + name + ".jpg"); String newPath = request.getContextPath()+"/" + getServletContext().getInitParameter("newsImgFolder") + "/" + name + ".jpg"; fileItem.write(file); String callback = request.getParameter("CKEditorFuncNum"); out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + newPath + "',''" + ")"); out.println("</script>"); out.flush(); } } catch (FileUploadException ex) { Logger.getLogger(DoUploadServlet.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(DoUploadServlet.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } } <div>} </div>
试试其它关键字
文件上传
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
biergaizi2009
贡献的其它代码
(
4
)
.
用N根长度相同的火柴,首尾顺次连接,摆成一个三角形
.
jQuery循环滚动展示代码
.
文件上传servlet处理代码
.
递归查找文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3