java的文件上傳

package com.here.Util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;


import com.cctv.costume.admin.config.Configure;


public class ImageUtil {
public static String buildZip(File imageFile) throws IOException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
String dateString = simpleDateFormat.format(new Date());
final Lock lock = new ReentrantLock();
String newName = null;
lock.lock();
try {
// 加鎖爲防止文件名重複
newName = System.currentTimeMillis() + ".zip";
} finally {
lock.unlock();
}

String suffix = dateString + File.separatorChar + newName;
String filePath = Configure.getConfig(Configure.PATH_VIDEO_FILE_PATH) + suffix;
String imageUrl = Configure.getConfig(Configure.PATH_VIDEO_PATH) + suffix;

File newImageFile = new File(filePath);
if (!newImageFile.getParentFile().exists()) {
newImageFile.getParentFile().mkdirs();
}
// 獲取文件輸出流
FileOutputStream fos = new FileOutputStream(newImageFile);
byte[] buffer = new byte[1024];
// 獲取內存中當前文件輸入流
InputStream in = new FileInputStream(imageFile);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
return filePath;
}



public static String buildVideo(File imageFile) throws IOException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
String dateString = simpleDateFormat.format(new Date());
final Lock lock = new ReentrantLock();
String newName = null;
lock.lock();
try {
// 加鎖爲防止文件名重複
newName = System.currentTimeMillis() + ".flv";
} finally {
lock.unlock();
}

String suffix = dateString + File.separatorChar + newName;
String filePath = Configure.getConfig(Configure.PATH_VIDEO_FILE_PATH) + suffix;
String imageUrl = Configure.getConfig(Configure.PATH_VIDEO_PATH) + suffix;

File newImageFile = new File(filePath);
if (!newImageFile.getParentFile().exists()) {
newImageFile.getParentFile().mkdirs();
}
// 獲取文件輸出流
FileOutputStream fos = new FileOutputStream(newImageFile);
byte[] buffer = new byte[1024];
// 獲取內存中當前文件輸入流
InputStream in = new FileInputStream(imageFile);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
// try {
// FileUtils.copyFile(imageFile, newImageFile);
// }catch (Exception e) {
// e.printStackTrace();
// }
return imageUrl;
}
public static String buildImage(File imageFile) throws IOException {
return buildImage(imageFile,false);
}
public static String buildImage(File imageFile,boolean genSmall) throws IOException {
String fileContent = getImageFormatName(imageFile);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
String dateString = simpleDateFormat.format(new Date());
final Lock lock = new ReentrantLock();
String newName = null;
lock.lock();
try {
// 加鎖爲防止文件名重複
newName = System.currentTimeMillis() + "." + fileContent;
} finally {
lock.unlock();
}
String suffix = dateString + File.separatorChar + newName;
String filePath = Configure.getConfig(Configure.PATH_IMAGE_FILE_PATH) + suffix;
// String imagePath = Configure.getConfig(Configure.PATH_IMAGE_FILE_PATH) + dateString;
String imageUrl = Configure.getConfig(Configure.PATH_IMAGE_PATH) + suffix;
// String imageUrl = imagePath + "/" + newName;
File newImageFile = new File(filePath);
if (!newImageFile.getParentFile().exists()) {
newImageFile.getParentFile().mkdirs();
}
// 獲取文件輸出流
FileOutputStream fos = new FileOutputStream(newImageFile);
byte[] buffer = new byte[1024];
// 獲取內存中當前文件輸入流
InputStream in = new FileInputStream(imageFile);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
if(genSmall){
try {
ImageConvertionUtil.convertThumbnail(newImageFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// try {
// FileUtils.copyFile(imageFile, newImageFile);
// }catch (Exception e) {
// e.printStackTrace();
// }
return imageUrl;
}



public static String buildImage(File imageFile, String fileName) throws IOException {
String fileContent = fileName.substring(fileName.lastIndexOf("."),fileName.length());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
String dateString = simpleDateFormat.format(new Date());
final Lock lock = new ReentrantLock();
String newName = null;
lock.lock();
try {
// 加鎖爲防止文件名重複
newName = System.currentTimeMillis() + fileContent;
} finally {
lock.unlock();
}
String suffix = dateString + "/" + newName;
String filePath = Configure.getConfig(Configure.PATH_IMAGE_FILE_PATH) + suffix;
// String imagePath = Configure.getConfig(Configure.PATH_IMAGE_FILE_PATH) + dateString;
String imageUrl = Configure.getConfig(Configure.PATH_IMAGE_PATH) + suffix;
// String imageUrl = imagePath + "/" + newName;
File newImageFile = new File(filePath);
if (!newImageFile.getParentFile().exists()) {
newImageFile.getParentFile().mkdirs();
}
// 獲取文件輸出流
FileOutputStream fos = new FileOutputStream(newImageFile);
byte[] buffer = new byte[1024];
// 獲取內存中當前文件輸入流
InputStream in = new FileInputStream(imageFile);
try {
int num = 0;
while ((num = in.read(buffer)) > 0) {
fos.write(buffer, 0, num);
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
in.close();
fos.close();
}
// try {
// FileUtils.copyFile(imageFile, newImageFile);
// }catch (Exception e) {
// e.printStackTrace();
// }
return imageUrl;
}


public static String getImageFormatName(File imageFile) {
try {
ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (!iterator.hasNext()) {
return null;
}
ImageReader imageReader = iterator.next();
imageInputStream.close();
return imageReader.getFormatName().toLowerCase();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

}

在Action層進行調用:

public String upload() throws IOException{
try {

//imagePath爲前端JSP頁面傳過來的文件屬性,需要跟前端的一致
exhiFlash.setImagePath(ImageUtil.buildImage(imagePath, getImagePathFileName()));
} catch (IOException e) {
e.printStackTrace();
}
//用來判斷上傳圖片文件的長和寬
BufferedImage reader=ImageIO.read(imagePath);
int width=reader.getWidth();
int height = reader.getHeight();
if(width !=998 && height != 425){
this.addActionMessage("請上傳尺寸爲998*425的圖片!");
return "add";
}
exhiFlash.setUploadTime(new Date());
flashService.doUpload(exhiFlash);
this.addActionMessage("上傳成功!");
return list();
}

JSP頁面:

<form action="upload" method="post" id="form" enctype="multipart/form-data">

<tr>
                  <td class="num1">FLASH圖片:</td>
                <td><input type="file" name="imagePath" size="46" id="imagePath"></td>  
                 </tr>

<script type="text/javascript">
function checkImg(){
var imagePath=document.getElementById("imagePath");
if (imagePath.value==""){
alert("請點擊瀏覽按鈕,選擇您要上傳的JPG或GIF文件或PNG文件!");
imagePath.select()
imagePath.focus();
return false;
}

if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(imagePath.value)) { 
  alert("圖片類型必須是.gif,jpeg,jpg,png中的一種"); 
  imagePath.select()
  document.execCommand("Delete");
  imagePath.focus();
  return false;
}
}

</script>

發佈了10 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章