服务器文件上传记录,记录一下上传文件(图片)的过程上传图片到ftp服务器

由于没理解servlet为何要用struts2取代而耗费了不少时间在找如何获取前端传输过来的数据。html

public void doPost(HttpServletRequest request,HttpServletResponse esponse) throws IOException,ServletException而转为struts2时,struts2给咱们处理了。

因此你须要在Action中定义他们,并且要和前端对应。

前端前端

文件

舒适提示:上传文件

Actionjava

package nwpu.precisefunds.compare.dao;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.Serializable;

import java.net.SocketException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.struts2.ServletActionContext;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;

import com.opensymphony.xwork2.ActionSupport;

import nwpu.precisefunds.applicant.service.ApplicantService;

import nwpu.precisefunds.photo.service.ImageService;

import nwpu.precisefunds.photo.vo.Image;

public class UploadImagesAction extends ActionSupport implements Serializable {

private static final int BUFFER_SIZE = 16 * 1024;

private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB

private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB

private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

private File photo;

private String photoContentType;

private String photoFileName;

private String destPath;

public File getPhoto() {

return photo;

}

public void setPhoto(File photo) {

this.photo = photo;

}

public String getPhotoContentType() {

return photoContentType;

}

public void setPhotoContentType(String photoContentType) {

this.photoContentType = photoContentType;

}

public String getPhotoFileName() {

return photoFileName;

}

public void setPhotoFileName(String photoFileName) {

this.photoFileName = photoFileName;

}

public String getDestPath() {

return destPath;

}

public void setDestPath(String destPath) {

this.destPath = destPath;

}

private String newFileName;

public String getNewFileName() {

return newFileName;

}

public void setNewFileName(String newFileName) {

this.newFileName = newFileName;

}

private ImageService imageService;

public ImageService getImageService() {

return imageService;

}

public void setImageService(ImageService imageService) {

this.imageService = imageService;

}

private ApplicantService applicantService;

public void setApplicantService(ApplicantService applicantService) {

this.applicantService = applicantService;

}

public String execute() throws SocketException, IOException {

HttpServletRequest request = ServletActionContext.getRequest();

//http://blog.163.com/lfsyhuangaigang@126/blog/static/77436670201332303122315/

//https://www.cnblogs.com/qinglangyijiu/p/7803818.html

FTPClient fc = new FTPClient();

int reply=0;

String hostName = "192.168.2.1";//服务器ip地址

String ftpUser = "ftptest";//ftp用户名

String ftpPass = "123456";//ftp密码

int port = 9091;//端口

fc.connect(hostName, port);

fc.login(ftpUser, ftpPass);

reply = fc.getReplyCode();

FTPReply.isPositiveCompletion(reply);

//System.out.println(reply+"是否是230呀");

if((FTPReply.isPositiveCompletion(reply))){

System.out.println("链接成功");

}

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

// 取当前日期做为文件夹

SimpleDateFormat cs = new SimpleDateFormat("yyyy");

String dateCs = cs.format(new Date());

System.out.println("文件夹格式为:"+dateCs);

String dateFile = sdf.format(new Date());

// System.out.println("格式化后的date:"+dateFile);

// 生成随机文件名

String fileName = UUID.randomUUID() + ".jpg";

System.out.println("文件名:" + fileName);

// 拼接上传的路径

String uploadPath = "uploadImage/" + dateFile + "/" + fileName;

System.out.println("上传的路径:"+uploadPath);

// 建立文件夹 必须一层一层建立

fc.makeDirectory("uploadImage");

fc.makeDirectory("uploadImage" + "/" + dateFile);

//https://www.cnblogs.com/kongxianghao/articles/6879367.html

//File f= new File("f:" + File.separator + "a.jpg");

InputStream input = new FileInputStream(getPhoto());

if (!(fc.changeWorkingDirectory(uploadPath))) {

fc.setFileType(FTPClient.BINARY_FILE_TYPE);

if (fc.storeFile(uploadPath, input)) {

// 上传成功将数据存到数据库

System.out.println("上传成功了?????????????");

Image img = new Image();// 须要补充内容

img.setImageTag("imageTag");

img.setXh("xh");

img.setImageUrl(uploadPath);

//int row = imageService.updateImage("xh", "imageTag", img);

}

request.getInputStream().close();

fc.logout();

}

return SUCCESS;

}

}

以上基本是主要的,数据库

struts2.xml配置struts2apache

/WEB-INF/jsp/user.jsp

applicationContext.xml配置Springtomcat

scope="prototype">

只是个人理解,本文章只做我的工做记录,但愿不要误导后来人。服务器

有些内容没有用到,等过几天再改一下,会添加用户的数据库内容。app

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值