<html:form action="/news/news.do" styleClass="form" enctype="multipart/form-data">
<html:file property="news.bigImage" styleClass="wid_innerpage1" style="width:362px;" styleId="bigImage" />
Whether Big News<input type= "checkbox" name="mainFlag" property = "news.mainFlag"/>
---------------------
FormFile bigImage = news.getBigImage(); //获取banner图片
String bigImagePath = SaveFile.saveFile(bigImage, "newsimage", savePath, "");
public static String saveFile(FormFile formFile,String middleName,String dir, String type) {
String result = "";
try {
if (formFile != null
&& formFile.getFileName() != null
&& formFile.getFileSize() > 0) {
String fileName = formFile.getFileName();
InputStream streamIn = formFile.getInputStream();
dir = dir + File.separator + middleName + File.separator;
if (!new File(dir).isDirectory()) {
new File(dir).mkdirs();
}
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyMMddhhmmss");
String newname = df.format(java.util.Calendar.getInstance().getTime());
newname = newname + type + fileName;
OutputStream streamOut = new FileOutputStream(dir + newname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
//返回的文件名包含了middleName ,middleName会在文件系统中形成一个目录
result = middleName + "/" + newname;
//开始将文件COPY到 应用服务器的webContent目录下
copyFile(formFile,middleName,newname);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
private static void copyFile(FormFile formFile, String middleName,String fileName) {
//通过配置文件获取保存图片的路径
Properties properties = UtilProperties.getProperties("upload.properties");
String savePath = properties.getProperty("resourcePath");
savePath = savePath + File.separator + middleName + File.separator;
if (!new File(savePath).isDirectory()) {
new File(savePath).mkdirs();
}
try {
InputStream streamIn = formFile.getInputStream();
OutputStream streamOut = new FileOutputStream(savePath + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
<html:file property="news.bigImage" styleClass="wid_innerpage1" style="width:362px;" styleId="bigImage" />
Whether Big News<input type= "checkbox" name="mainFlag" property = "news.mainFlag"/>
---------------------
FormFile bigImage = news.getBigImage(); //获取banner图片
String bigImagePath = SaveFile.saveFile(bigImage, "newsimage", savePath, "");
public static String saveFile(FormFile formFile,String middleName,String dir, String type) {
String result = "";
try {
if (formFile != null
&& formFile.getFileName() != null
&& formFile.getFileSize() > 0) {
String fileName = formFile.getFileName();
InputStream streamIn = formFile.getInputStream();
dir = dir + File.separator + middleName + File.separator;
if (!new File(dir).isDirectory()) {
new File(dir).mkdirs();
}
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyMMddhhmmss");
String newname = df.format(java.util.Calendar.getInstance().getTime());
newname = newname + type + fileName;
OutputStream streamOut = new FileOutputStream(dir + newname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
//返回的文件名包含了middleName ,middleName会在文件系统中形成一个目录
result = middleName + "/" + newname;
//开始将文件COPY到 应用服务器的webContent目录下
copyFile(formFile,middleName,newname);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
private static void copyFile(FormFile formFile, String middleName,String fileName) {
//通过配置文件获取保存图片的路径
Properties properties = UtilProperties.getProperties("upload.properties");
String savePath = properties.getProperty("resourcePath");
savePath = savePath + File.separator + middleName + File.separator;
if (!new File(savePath).isDirectory()) {
new File(savePath).mkdirs();
}
try {
InputStream streamIn = formFile.getInputStream();
OutputStream streamOut = new FileOutputStream(savePath + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
}catch (Exception e) {
System.out.println(e.getMessage());
}
}