文件上传一
来源:互联网 发布:mac共享windows文件夹 编辑:程序博客网 时间:2024/05/16 16:17
public
class
Upload
extends
ActionSupport {
private
File[] image ;
//多个文件 ,如果单个文件 ,就定义成 File image ;
private
String[] imageFileName ;
//多个文件名 ,如果单个文件 ,就定义成 String imageFileName ;
private
String[] imageContentType ;
//多个文的内容类型,如果 单个文件就定义成 String imageContentType
get(),set() ....
/**
*其中image为jsp中的File 的name
*<input type="file" name="image" /><br />
*/
public
String execute()
throws
Exception {
String realpath = ServletActionContext.getServletContext().getRealPath(
"/images"
) ;
System.out.println(
"Real Path : "
+ realpath );
if
(image !=
null
){
File savedir =
new
File(realpath) ;
if
(! savedir.exists()) {
savedir.mkdirs() ;
}
// File savefile = new File(realpath, this.imageFileName ) ;
for
(
int
i =
0
; i < image.length; i++){
File savefile =
new
File(realpath, imageFileName[i]) ;
FileUtils.copyFile(image[i], savefile) ;
System.out.println(imageContentType[i]);
}
ActionContext.getContext().put(
"message"
,
"上传成功:"
+ realpath) ;
}
return
SUCCESS ;
}
}
方法一:
ActionContext ctx=ActionContext.getContext();
HttpServletRequest request=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
方法二:
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
方法三:
实现ServletRequestAware接口,并实现
public void setServletRequest(HttpServletRequest request) ;方法
public String addLinkMan() throws IOException {
//判断是否需要上传文件
if(upload != null) {//上传
//上传逻辑
//1 在服务器文件夹里面创建文件
File serverFile = new File("I:\\crm"+"/"+uploadFileName);
//2 把本地文件复制到服务器文件中
//提供工具类,直接调用工具类方法实现
//两个参数:第一个参数本地文件,第二个参数服务器文件
FileUtils.copyFile(upload, serverFile);
}
//获取选择客户的cid值
// String scid = ServletActionContext.getRequest().getParameter("cid");
// // cid如何处理
// // 把cid值封装到linkman对象里面
// // 把cid放到linkman的customer对象里面
// int cid = Integer.parseInt(scid);
// //放到customer里面
// Customer c = new Customer();
// c.setCid(cid);
// //把c放到linkman里面
// linkMan.setCustomer(c);
//调用service的方法联系人信息添加到数据库里面
linkManService.add(linkMan);
return "addLinkMan";
}
- 文件上传(一)
- 文件上传(一)
- FTP文件上传(一)
- 文件上传入门一
- SpringMVC文件上传(一)
- 文件上传一
- FastDFS上传文件一 : 单文件上传
- 文件上传(一)原生文本文件上传
- Struts2 文件上传(一)
- struts2实现文件上传(一)
- 文件的上传(一)
- Struts2文件上传(一)
- struts2文件上传(一)
- ASIHTTPRequest系列(一):文件上传
- 文件处理一(上传)
- php上传文件(一)
- android retrofit2 文件上传(一)
- FTP服务器文件操作 一:文件上传
- 算法训练 暗恋
- 利用FFMPEG进行视频分割
- php读取mysql分页查询
- 用《内网穿山甲》共享内网中的远程桌面服务
- php插入mysql数据返回id
- 文件上传一
- Linux实现进度条小程序
- Java简介
- 8086汇编7:跳转指令
- Docker 节点在 ODL 控制下的 OVS 网桥连接
- tomcat——简单的Web服务器和Servlet容器
- 数学基础 -- 基础定义(集合、实数集、映射、函数)
- CQOI2014 排序机械臂--splay膜版题
- php查询mysql分页查询返回数组