/** * */ package com.chen.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.chen.bean.Users; import com.chen.service.UserService; /** * @author phn * */ public class FileUpload { private Users user; private UserService userService; private File fileUpload; private String fileUploadFileName; private String fileUploadContentType; private String savePath; DateFormat df = new SimpleDateFormat("yyyyMMdd-hh-mm-ss-"); String questionTime = df.format(new Date()); HttpServletRequest request = ServletActionContext.getRequest(); public File getFileUpload() { return fileUpload; } public void setFileUpload(File fileUpload) { this.fileUpload = fileUpload; } public String getFileUploadFileName() { return fileUploadFileName; } public void setFileUploadFileName(String fileUploadFileName) { this.fileUploadFileName = fileUploadFileName; } public String getFileUploadContentType() { return fileUploadContentType; } public void setFileUploadContentType(String fileUploadContentType) { this.fileUploadContentType = fileUploadContentType; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public String userPicUpload() throws Exception { Random r = new Random(); // 以服务器的文件保存地址和原文件名建立上传文件输出流 FileOutputStream fos = new FileOutputStream(ServletActionContext .getRequest().getRealPath(savePath) + "\\" + questionTime + getFileUploadFileName()); // 以上传文件建立一个文件上传流 FileInputStream fis = new FileInputStream(getFileUpload()); // 将上传文件的内容写入服务器 byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } String p = request.getContextPath() + this.savePath + "/" + questionTime + getFileUploadFileName().toString(); HttpSession session = ServletActionContext.getRequest().getSession(); user = (Users) session.getAttribute("tu"); user.setPicture(p); if (this.userService.update(user)) { return "fileupload_ok"; } return "fileupload_no"; } public Users getUser() { return user; } public void setUser(Users user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
最近下载更多
123ggf LV8
2023年6月14日
limin123 LV6
2023年6月11日
0011100 LV12
2023年5月31日
微信网友_6495054193971200 LV2
2023年5月29日
1796527961 LV1
2023年3月23日
wouldbb LV6
2023年3月21日
tyyeng LV18
2023年1月1日
long123_356 LV5
2022年12月10日
akbar2020 LV9
2022年9月4日
yazhiyjs LV4
2022年8月30日
最近浏览更多
Rinkia
3月28日
暂无贡献等级
xiaopengzep LV1
3月24日
lvhongquan LV11
2023年12月31日
没有的没有
2023年12月27日
暂无贡献等级
wutongzui11
2023年12月13日
暂无贡献等级
云着殇 LV8
2023年12月13日
空中飞尘 LV13
2023年12月13日
唐唐丶 LV33
2023年12月3日
hongdongdong LV12
2023年12月2日
微信网友_6465435620184064 LV6
2023年10月24日