level 1
小酒窝的Dream
楼主
哪位好心人能告诉一下 贴上了上传附件的代码
*
* 上传
*/
public void importData(File file, Mtlog mtlog, String fileImportFileName,
ServletRequest req) throws IOException {
// 获取会话传输的地址
String path = req.getRealPath("/");
//String path = realPath;
// 基本文件属性的配置
if(fileImportFileName!=null){
// 新文件的的文件夹设置
String uploadPath = path+ "pages\\uploadFiles\\office\\"+mtlog.getMtlogId()+"\\";
// 文件再数据库中存储地址
String accPath = "pages/uploadFiles/office/"+mtlog.getMtlogId()+"/";
// 如果存放的文件夹不存在,则新建
File fileNew = new File(uploadPath);
if (!fileNew.exists()) {
fileNew.mkdir();
}
// 设置上传文件路径(用于新建文件)
path = uploadPath + fileImportFileName;
File newFile = new File(path);
// 传输文件
InputStream in = null;
OutputStream out = null;
byte[] buffer = new byte[1024];
try {
in = new BufferedInputStream(new FileInputStream(file), 1024);
out = new BufferedOutputStream(new FileOutputStream(newFile), 1024);
while (in.read(buffer) > 0) {
out.write(buffer);
}
} catch (Exception e) {
throw new ExtFileUploadException(GrmErrorEnum.FIEL_UPLOAD_ERROR);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
throw new ExtFileUploadException(
GrmErrorEnum.FIEL_UPLOAD_ERROR);
}
}
}
// 设置传入NoticeCtrl对象的附件路径
mtlog.setPath(accPath + fileImportFileName);
}
}
2014年06月12日 03点06分
1
*
* 上传
*/
public void importData(File file, Mtlog mtlog, String fileImportFileName,
ServletRequest req) throws IOException {
// 获取会话传输的地址
String path = req.getRealPath("/");
//String path = realPath;
// 基本文件属性的配置
if(fileImportFileName!=null){
// 新文件的的文件夹设置
String uploadPath = path+ "pages\\uploadFiles\\office\\"+mtlog.getMtlogId()+"\\";
// 文件再数据库中存储地址
String accPath = "pages/uploadFiles/office/"+mtlog.getMtlogId()+"/";
// 如果存放的文件夹不存在,则新建
File fileNew = new File(uploadPath);
if (!fileNew.exists()) {
fileNew.mkdir();
}
// 设置上传文件路径(用于新建文件)
path = uploadPath + fileImportFileName;
File newFile = new File(path);
// 传输文件
InputStream in = null;
OutputStream out = null;
byte[] buffer = new byte[1024];
try {
in = new BufferedInputStream(new FileInputStream(file), 1024);
out = new BufferedOutputStream(new FileOutputStream(newFile), 1024);
while (in.read(buffer) > 0) {
out.write(buffer);
}
} catch (Exception e) {
throw new ExtFileUploadException(GrmErrorEnum.FIEL_UPLOAD_ERROR);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
throw new ExtFileUploadException(
GrmErrorEnum.FIEL_UPLOAD_ERROR);
}
}
}
// 设置传入NoticeCtrl对象的附件路径
mtlog.setPath(accPath + fileImportFileName);
}
}