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 com.arivoc.accentz.action.BaseAction;
public class UploadFileUnit extends BaseAction {
private static final long serialVersionUID = 3891999748001909716L;
private static final int BUFFER_SIZE = 16 * 1024;
/**
* 文件上传
* @param src 源文件
* @param dst 目标文件
* @return
* @throws Exception
*/
public boolean upload(File src, File dst) throws Exception {
boolean mResult = false;
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);
}
mResult = true;
} catch (Exception e) {
mResult = false;
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return mResult;
}
}
java I/O 数据流 文件上传
最新推荐文章于 2023-01-05 17:24:02 发布