java实现文件上传
private File photo;
private String photoFileName; // 封装上传文件名的属性
private String root = "D:/root/pic";
private void loadPicture() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
FileOutputStream fos;
// 获取图片后缀名
String partRightType = "";
String savePath = "";
if (photoFileName != null && !"".equals(photoFileName)) {
partRightType = photoFileName.substring(photoFileName
.lastIndexOf("."));
}
// 判断图片的格式
if (!ImageFile.checkImageType(partRightType)) {
// 跳转到添加页面
request.setAttribute("info", "The file format is incorrect!");
} else {
try {
photoFileName = DateUtils.getDateNoStyle() + "-"
+ UUID.randomUUID() + partRightType;
savePath = root + "/";
File uploadFilePath = new File(savePath);
if (uploadFilePath.exists() == false) {
uploadFilePath.mkdirs();
System.out.println("路径不存在,但是已经成功创建了" + savePath);
} else {
System.out.println("路径存在了" + savePath);
}
fos = new FileOutputStream(new File(savePath + photoFileName));
FileInputStream fis = new FileInputStream(getPhoto());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
} catch (FileNotFoundException foe) {
System.out.println("上传文件为0字节");
}
// Pdf2Swf.convertPDF2SWF(savePath + uploadFileName, savePath,
// uploadFileName.replaceAll(".pdf", ".swf"));
//
// String path = "/uploadFiles/pdf/" + periodical.getCountry() + "/"
// + uploadFileName.replaceAll(".pdf", ".swf");
}
}

浙公网安备 33010602011771号