上传文件
参考文章有三种上传方式http://wiki.jikexueyuan.com/project/ssh-noob-learning/upload-file.html
1:在struts中配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.il8n.encoding" value="UTF-8" />
<constant name="struts.action.extension" value=","></constant>
<!-- 上传文件最大限制(如果为多文件上传,则为多个文件的总大小) -->
<constant name="struts.multipart.maxSize" value="40000000"></constant>
<!-- 存放上传文件的临时目录 -->
<constant name="struts.multipart.saveDir" value="D:\\temp"></constant>
<!-- 注意使用的包不是struts-default 而是json-default -->
<package name="myPackage" namespace="/" extends="json-default">
<action name="upImgAction" class = "infoAction" method="upImg">
<!-- param是参数,能够到action中 -->
<param name="maximumSize ">1000000</param>
<!-- 这里写需要的格式待会儿会用到检验格式 -->
<param name="allowedTypes">image/png,image/jpg,image/jpeg,image/gif</param>
</action>
</package>
</struts>
2:action
采用表单注入
//上传文件,采用表单形式,注入
private File upload; //上传文件
private String uploadFileName; //上传文件名
private String uploadContentType; //上传文件类型
private long maximumSize;
private String allowedTypes;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public long getMaximumSize() {
return maximumSize;
}
public void setMaximumSize(long maximumSize) {
this.maximumSize = maximumSize;
}
public String getAllowedTypes() {
return allowedTypes;
}
public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}
//上传头像
public String upImg() throws IOException {
System.out.println("upImg...action...");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin", "*");
PrintWriter outt = response.getWriter();
JSONObject json = new JSONObject();
try{
//上传文件
String flag = infoService.upFile(maximumSize,allowedTypes,upload,uploadFileName,uploadContentType);
//地址为上传成功 0失败 -1太大 -2类型不符合
json.put("msg", flag);
} catch (Exception e) {
System.out.println(e.toString());
json.put("msg", "0"); //失败
} finally {
outt.write(json.toString());
outt.flush();
outt.close();
}
return null;
}
}
3:service中
//上传文件
public String upFile(long maximumSize, String allowedTypes, File upload, String uploadFileName,
String uploadContentType) {
System.out.println("upFile...service...");
HttpSession session = ServletActionContext.getRequest().getSession();
try{
//新建文件
File uploadFile = new File(ServletActionContext.getServletContext().getRealPath("upload"));
if(!uploadFile.exists()) {
uploadFile.mkdir();
}
//验证文件大小
if (maximumSize < upload.length()) {
return "-1"; //文件过大
} else {
//验证文件格式
boolean flag = false;
String[] allowedTypesStr = allowedTypes.split(","); //这是在配置文件中 按照逗号隔开
for (int i = 0; i < allowedTypesStr.length; i++) {
if (uploadContentType.equals(allowedTypesStr[i])) {
flag = true;
}
}
if (flag == false) {
return "-2"; //文件格式错误
} else {
//文件名为"uid_img"+uid+"后缀"
String[] uploadFileNameArray = uploadFileName.split("\\.");
uploadFileName = "uid_img" + session.getAttribute("uid") + "." + uploadFileNameArray[1];
String path = uploadFile.getPath()+ "\\" + uploadFileName;
//准备上传
FileUtils.copyFile(upload, new File(path));
//删除临时文件
upload.delete();
//相对路径
String rePath = "upload" + "\\" + uploadFileName;
//写入数据库
boolean flag2 = infoDao.upImg((int)session.getAttribute("uid"), rePath);
if(flag2 == false) {
return "0"; //失败
} else {
System.out.println(ServletActionContext.getServletContext().getContextPath());
return rePath; //成功
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
return "0";
}
}
4:dao
//写入头像地址
@Override
public boolean upImg(int uid, String path) {
System.out.println("upImg...dao...");
try{
User user = hibernateTemplate.get(User.class, uid);
user.setPhoto(path);
return true;
}catch (Exception e) {
System.out.println(e.toString());
return false;
}
}