1、前台:
jsp
js
var limitSize = 1 * 1024 * 1024;
$scope.$watch("data.photo_file", function (newValue, oldValue) {
if ($scope.data && $scope.data.photo_file) {
if (!oldValue || newValue.size != oldValue.size) {
if (newValue.size <= limitSize) {
var file = document.getElementById("file").files[0];
$("#photo").attr("src", window.URL.createObjectURL(file));
} else {
alert("图片大小不得超过1M!");
}
}
}
});
<script src="bower_components/angular-file-upload/dist/angular-file-upload.min.js">script>
3、在build.gradle配置文件中添加上传所需要的包。
compile group: 'commons-fileupload', name: 'commons-fileupload', version:'1.3.1'
compile group: 'commons-io', name: 'commons-io', version:'2.4'
4、后台
public ViewData getXXX(@RequestParam MultipartFile[] photo_file, String xx, String xx, String xx, String xx, String xx,
HttpServletRequest request) {
ViewData vd = new ViewData();
String base64photo = "";
if (photo_file != null && photo_file.length > 0) {
// System.out.println("文件大小:" + photo_file[0].getSize());
MultipartFile file = photo_file[0];
if (file.getSize() < 1 * 1024 * 1024) {
//获取图片的字节流
byte[] bytePhoto = FileUtils.file2Byte(photo_file[0]);
//压缩图片
base64photo = FileUtils.resize(bytePhoto, 300, 0.7f);
// System.out.println("字符长度:" + base64photo.length());
} else {
return vd.error("照片大小不能超过1M!");
}
}
....
}
FileUtils
/** 文件转字节
* @param file
* @return
*/
public static byte[] file2Byte(MultipartFile file) {
byte[] buffer = null;
ByteArrayOutputStream bos = null;
try {
InputStream inputStream = file.getInputStream();
bos = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
int n;
while ((n = inputStream.read(b)) != -1) {
bos.write(b, 0, n);
}
file.getInputStream().close();
bos.close();
buffer = bos.toByteArray();
// BASE64Encoder encoder = new BASE64Encoder();
// base64photo = encoder.encode(buffer);
// System.out.println("照片流64:" + encoder.encode(buffer));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null) { bos.close(); }
} catch (IOException e) {
}
}
return buffer;
}
/**
* 压缩图片
* @param b 图片的字节流
* @param newWidth 压缩的宽度
* @param quality 压缩质量 0-1之间float类型
* @return
* @throws IOException
*/
public static String resize(byte[] b,int newWidth, float quality){
if (quality > 1) {
throw new IllegalArgumentException("Quality has to be between 0 and 1");
}
String baseStr = "";
ByteArrayOutputStream byteArrayOutputStream = null;
try{
ImageIcon ii = new ImageIcon(b);
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth,(newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = {0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0};
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
byteArrayOutputStream = new ByteArrayOutputStream(4096);
// Encodes image as a JPEG data stream
JPEGImageEncoder imgEncoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);
JPEGEncodeParam param = imgEncoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
imgEncoder.setJPEGEncodeParam(param);
imgEncoder.encode(bufferedImage);
//转换成64字节码
BASE64Encoder encoder = new BASE64Encoder();
baseStr = encoder.encode(byteArrayOutputStream.toByteArray());
}catch (IOException e){
e.printStackTrace();
}
// File resizedFile = new File("/Users/jennifer/Documents/11111.jpg");
// FileOutputStream out = new FileOutputStream(resizedFile);
// out.write(byteArrayOutputStream.toByteArray());
// out.close();
return baseStr;
} // Example usage