📄 lotteryuploadaction.java
字号:
package com.szhelper.lotteryWeb.webapp.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 org.apache.struts2.ServletActionContext;
public class LotteryUploadAction extends LotteryWebBaseActionWrap {
private File upload;// uploaded file object
private String uploadFileName; // uploaded file name
private String uploadContentType; // uploaded file type
private String savePath;
protected static final int BUFFER_SIZE = 16 * 1024;
public String upload() {
System.out.println("upload ...");
copyToDst();
return SUCCESS;
}
private String getDstPath() {
String dstPath = ServletActionContext.getServletContext().getRealPath(
this.getSavePath())
+ "\\" + this.getUploadFileName();
return dstPath;
}
protected void copyToDst() {
copyToDst(getDstPath());
}
protected void copyToDst(String dst) {
File dstFile = new File(dst);
copyToDst(getUpload(), dstFile);
}
protected void copyToDst(File dst) {
copyToDst(getUpload(), dst);
}
protected void copyToDst(File src, File dst) {
if (src == null) {
System.out.println("ignore upload image");
return;
}
System.out.println("uploading image ...");
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
LOG.error(e);
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
LOG.error(e);
}
}
}
}
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 String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -