您的位置:首页 > 其它

SSH整合问题&&图片上传问题

2017-07-16 00:00 316 查看
在做基于SSH客户管理系统的文件上传,将磁盘上的图片上传到tomcat中保存在指定目录下

期间出现路径错误的问题,在数据库中存入的是相对路径

具体上传步骤:

第一步:在form标签下,添加一个属性,enctype="multipart/form-data", 要注意的一点就是<input type="file" name="upload"/>中的name的命名要与action中File的对象名保持一致.(注意红色代码部分)

<FORM id=form1 name=form1
action="${pageContext.request.contextPath }/customer_save.action"
method=post enctype="multipart/form-data">
<TR>
<td>上传文件:</td>
<td><input type="file" name="upload"></td>
</TR>
</FORM>

第二步: 处理Action中(CustomerAction.java):

package com.tbamaw.web.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.tbamaw.domain.Customer;
import com.tbamaw.domain.Dictionary;
import com.tbamaw.domain.PageBean;
import com.tbamaw.service.CustomerService;
import com.tbamaw.utils.UploadUtils;

/**
* 客户控制层
* @author bamaw
*
*/
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

private static final long serialVersionUID = 1L;
//切勿忘记手动new
private Customer customer = new Customer();
//model是CustomerAction类的属性-可以通过其在值栈中拿值
public Customer getModel() {
return customer;
}

//提供service成员方法 提供set方法
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}

/*
* 文件上传时,需要在Action中定义成员属性且命名是有规范的
* 在上传文件的的界面中定义的name属性值是什么,这块就是upload
*/

private File upload;//要上传的文件
private String uploadFileName;//上传文件的名称--没有中文乱码
@SuppressWarnings("unused")
private String uploadContentType;//上传文件的类型
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

/**
* 保存客户
* @return
* @throws IOException
*/
public String save() throws IOException{

//判断文件是否上传以及之后的操作
if (uploadFileName != null) {
//处理文件的名称
String uuidUploadFileName = UploadUtils.getUUIDName(uploadFileName);
System.out.println(uuidUploadFileName);
//获得当前项目文件上传磁盘的绝对路径
String realPath = ServletActionContext.getServletContext().getRealPath("upload-img");
//创建一个文件
File file = new File(realPath+"\\"+uuidUploadFileName);
/**
* FileUtils的copyFile方法,可以简便的实现文件从一个目录上传到另一个目录,
* 其中第一个参数是源文件File类型对象,第二个参数是目标文件File类型对象.
* 这里其实就模拟了从本地上传到服务器的过程,注意参数类型都是File类型.
*/
//文件上传使用FileUtils工具类
FileUtils.copyFile(upload, file);
///ssh_crm/WebRoot/upload-img
//把上传文件的路径保存到客户表中
//customer.setCust_file_upload("upload-img" + File.separator + uuidUploadFileName);
customer.setCust_file_upload("upload-img"+"\\"+uuidUploadFileName);

}
customerService.save(customer);
return "saveSuccess";
}
}

注意:我项目中存放图片的路径是WebRoot下的upload-img文件夹,执行完操作后,如果发现myeclipse中该文件夹中没有你上传的图片,先不要怀疑代码是否错了,应该先去tomcat安装目录下找到该项目的upload-img文件夹,图片会上传到那里,只要tomcat中上传成功了,就可以用了.

需要注意的地方有以下几点:

action中需要定义三个属性 : 1.上传的文件File 2.上传的文件名, 3.上传文件的MIME类型.

File类型的对象名需要和jsp中type="file"的input的name属性值相同.

String类型文件名=File对象名+FileName.

获得当前项目文件上传磁盘的绝对路径String realPath = ServletActionContext.getServletContext().getRealPath("upload-img");

FileUtils的copyFile(xxx,xxx)方法 可以实现文件从一个目录上传到另一个目录,其中第一个参数是源文件File类型对象,第二个参数是上传文件File类型对象.从本地上传到服务器的过程,注意参数类型都是File类型.

注意在数据库中存入图片路径是相对路径; 例如:customer.setCust_file_upload("upload-img" + File.separator + uuidUploadFileName); 其中File.separator相当于:customer.setCust_file_upload("upload-img"+"\\"+uuidUploadFileName); 中的 File.separator >>>>> "\\"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件上传