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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章