package com.action;
import java.io.*;
import java.util.*;
import com.entity.*;
import com.service.*;
import com.service.impl.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddNewAction extends HttpServlet {
/**
* Constructor of the object.
*/
public AddNewAction() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
boolean ckUpload = ServletFileUpload.isMultipartContent(request);
String ntid ="";
String ntitle ="";
String nauthor ="";
String nsummary ="";
String ncontent ="";
String npicpath ="";
// request.getParameter(arg0) 不能用
if(ckUpload){
//1. 文件上传
boolean ckFile = true;//文件是否上传成功
try{
DiskFileItemFactory df = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(df);//上传组件
String filePath = request.getServletContext().getRealPath("upload/");
System.out.println("----filePath = "+filePath);
//设置上传文件的大小
//sfu.setSizeMax(sizeMax);
sfu.setFileSizeMax(500*1024*1024);
//解析请求
List<FileItem> flist = sfu.parseRequest(request);
for(FileItem fit : flist){
if(fit.isFormField()){ // 普通组件: text,checkbox,radio,password
//取表单元素的值
if(fit.getFieldName().equals("ntid")){
ntid = fit.getString("GBK");
}
if(fit.getFieldName().equals("ntitle")){
ntitle = fit.getString("GBK");
}
if(fit.getFieldName().equals("nauthor")){
nauthor = fit.getString("GBK");
}
if(fit.getFieldName().equals("ncontent")){
ncontent = fit.getString("GBK");
}
if(fit.getFieldName().equals("nsummary")){
nsummary = fit.getString("GBK");
}
}else{ // 文件组件 : input type= file
//取文件名
String filename = fit.getName();
/* bak.txt
* d:\\temp\\bak.txt
*/
int index = filename.lastIndexOf("\\");
if(index !=-1){
filename = filename.substring(index+1);
}
//随机数
String rand = UUID.randomUUID().toString();
filename = rand +"_"+ filename;
System.out.println("----filename = "+filename);
//设置web 访问路径
npicpath = "/news/upload/"+filename;
//新建文件
File f2 = new File(filePath,filename);
//写文件
fit.write(f2);
}
}
}catch(Exception e){
e.printStackTrace();
ckFile = false;//文件上传失败
}
//2. 进数据库 (必须文件上传无误)
if(ckFile){
System.out.println(" --- npicpath = "+npicpath);
}
//3. 最后要去新闻列表页面
}else{
throw new ServletException("----enctype != multipart/from-data");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java实现网页新闻程序
共113个文件
class:25个
java:25个
gif:23个
需积分: 10 10 下载量 33 浏览量
2017-10-25
10:50:53
上传
评论 3
收藏 2.71MB RAR 举报
温馨提示
*************不要问我是谁。请叫我雷锋!************
资源推荐
资源详情
资源评论
收起资源包目录
java实现网页新闻程序 (113个子文件)
TopicDao.class 4KB
News.class 3KB
IndexAction.class 3KB
LoginAction.class 2KB
TopicListAction.class 2KB
AddNewAction.class 2KB
UpdateTopicAction.class 2KB
DelTopicAction.class 2KB
OneTopicAction.class 2KB
AddTopicAction.class 2KB
TestAction.class 2KB
UserDao.class 2KB
JdbcUtil.class 1KB
TopicService.class 1KB
AppUtil.class 1KB
Users.class 1KB
Test1.class 955B
Topic.class 868B
Test2.class 744B
UserService.class 644B
BaseDao.class 575B
ITopicService.class 382B
ITopicDao.class 370B
IUserService.class 194B
IUserDao.class 182B
.classpath 1KB
org.eclipse.wst.common.component 471B
org.eclipse.wst.jsdt.ui.superType.container 49B
admin.css 3KB
main.css 3KB
read.css 3KB
main-1.gif 290KB
head.gif 40KB
a_b01.gif 40KB
class_bg.gif 6KB
sidebarbg.gif 5KB
class_type.gif 4KB
title_3.gif 4KB
title_1.gif 4KB
title_2.gif 3KB
friend_ico.gif 3KB
opt_name.gif 3KB
channel_bg.gif 1KB
friend_logo.gif 1KB
login_sub.gif 987B
login_input.gif 504B
firend_bg.gif 343B
topbg.gif 195B
admin_bar.gif 191B
friend_t.gif 189B
opt_sub.gif 180B
nav_leftdot.gif 82B
nav_leftline.gif 53B
arrow.gif 50B
ojdbc14.jar 1.48MB
mail.jar 445KB
commons-io-2.4.jar 181KB
commons-fileupload-1.2.2.jar 58KB
AddNewAction.java 5KB
TopicDao.java 4KB
LoginAction.java 3KB
TestAction.java 3KB
IndexAction.java 3KB
TopicListAction.java 3KB
UpdateTopicAction.java 2KB
AddTopicAction.java 2KB
OneTopicAction.java 2KB
DelTopicAction.java 2KB
News.java 2KB
JdbcUtil.java 981B
UserDao.java 787B
TopicService.java 696B
AppUtil.java 683B
Users.java 611B
Topic.java 437B
ITopicService.java 366B
ITopicDao.java 353B
UserService.java 347B
Test1.java 316B
BaseDao.java 253B
Test2.java 224B
IUserService.java 161B
IUserDao.java 151B
Picture1.jpg 51KB
Picture2.jpg 50KB
Picture4.jpg 47KB
Picture3.jpg 31KB
banner.jpg 20KB
logo.jpg 14KB
left.jpg 6KB
logo1.jpg 2KB
jquery-1.8.3.min.js 91KB
.jsdtscope 522B
index.jsp 8KB
news_read.jsp 7KB
admin.jsp 6KB
news_modify.jsp 6KB
topic_list.jsp 6KB
news_add.jsp 5KB
topic_modify.jsp 3KB
共 113 条
- 1
- 2
资源评论
qq_39600324
- 粉丝: 4
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功