//设备图片
$('#image').val(data_.image);
$('#imageName').html(data_.image);
$('#imageView').attr('src', '/imgs/'+data_.image);
package com.iqj.project.controller.admin;
import com.iqj.project.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
* 图片读取
*/
@Controller
@RequestMapping("/imgs")
public class ImageReadController {
@Value("${iqj.upload.image.path}")
private String imagePath; //图片存储路径
@RequestMapping("/{file}.{type}")
public void image(@PathVariable(name="file") String file,
@PathVariable(name="type") String type,
HttpServletResponse response) throws IOException {
String fileName = imagePath + file + "." + type;
try {
responseImage(response, FileUtils.toByteArray(fileName));
} catch (IOException e) {
/*==============================*/
/*
处理找不到图片后返回默认图片问题
@time 2018年04月11日11:25:23
*/
/*==============================*/
File defaultImage = ResourceUtils.getFile("classpath:static/images/noimage.png");
responseImage(response, FileUtils.toByteArray(defaultImage.getPath()));
}
}
/**
* 通过response返回图片ResponseUtil
* @param response HttpServletResponse对象
* @param imageByte 图片字节集
* @throws IOException
*/
public void responseImage(HttpServletResponse response, byte[] imageByte) throws IOException {
// 设置ContentType
response.setContentType("image/*");
// 获取输出流
OutputStream stream = response.getOutputStream();
// 输入字节
stream.write(imageByte);
}
}
package com.iqj.project.util;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class FileUtils {
public byte[] getContent(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
/**
* the traditional io way
*
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray(String filename) throws IOException {
File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}
BufferedInputStream in = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length())) {
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
/**
* NIO way
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray2(String filename) throws IOException {
File f = new File(filename);
if (!f.exists()) {
throw new FileNotFoundException(filename);
}
FileChannel channel = null;
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
channel = fs.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
while ((channel.read(byteBuffer)) > 0) {
// do nothing
// System.out.println("reading");
}
return byteBuffer.array();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
*
* @param filename
* @return
* @throws IOException
*/
public static byte[] toByteArray3(String filename) throws IOException {
FileChannel fc = null;
try {
fc = new RandomAccessFile(filename, "r").getChannel();
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
byte[] result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
// System.out.println("remain");
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件内容
*
* @param file 文件
* @param charset 编码
* @return
*/
public static String readFile(File file, String charset) {
//设置默认编码
if (charset == null) {
charset = "UTF-8";
}
if (file.isFile() && file.exists()) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text = null;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
return sb.toString();
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
/**
* 写入TXT,覆盖原内容
*
* @param content
* @param fileName
* @return
* @throws Exception
*/
public static boolean writeTxtFile(String content, File fileName) throws Exception {
RandomAccessFile mm = null;
boolean flag = false;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(content.getBytes("UTF-8"));
fileOutputStream.close();
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}