@RequestMapping(value = "/loan/doUpload", method = RequestMethod.POST)
// @ResponseBody
public String upload(@RequestParam(value = "file", required = false) MultipartFile file,HttpSession session, HttpServletResponse response)
throws IOException {
LoanFile loanFile = null;
try{
Customer customer = (Customer)session.getAttribute(Constants.SESSION_USER);
loanFile = loanService.doUpload(file, customer);
}catch(Exception e){
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
String textFiles = JsonUtil.getJsonStringGeneric(loanFile);
response.getWriter().write("<span>"+textFiles+"</span>");
return null;
}
@RequestMapping(value = "/laon/download")
public void download(@RequestParam(value="id") int id, HttpServletResponse response){
LoanFile loanFile = null;
try{
loanFile = loanService.getLoanFileById(id);
loanService.doDownload(loanFile.getFilePath(), loanFile.getFileFullName(), response);
}catch(Exception e){
e.printStackTrace();
}
}
@RequestMapping(value = "/loan/getPhotoById")
public void getPhotoById (int id, final HttpServletResponse response){
LoanFile loanFile = loanService.getLoanFileById(id);
File file = new File(loanService.getRootPath()+loanFile.getFilePath());
if (file.exists())
{
FileInputStream out = null;
try
{
out = new FileInputStream(file);
byte[] data = new byte[loanFile.getFileSize()];
out.read(data);
response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
OutputStream outputSream = response.getOutputStream();
InputStream in = new ByteArrayInputStream(data);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputSream.write(buf, 0, len);
}
outputSream.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
@RequestMapping(value = "/loan/getPhotoByIdandSize")
public void getPhotoById (int id, int width, int height, final HttpServletResponse response){
LoanFile loanFile = loanService.getLoanFileById(id);
File file = new File(loanService.getRootPath() + loanFile.getFilePath());
if (file.exists()) {
FileInputStream out = null;
try {
out = new FileInputStream(file);
byte[] data = new byte[loanFile.getFileSize()];
out.read(data);
data = scaleImage(data, width, height);
response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
OutputStream outputSream = response.getOutputStream();
InputStream in = new ByteArrayInputStream(data);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputSream.write(buf, 0, len);
}
outputSream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public static byte[] scaleImage(byte[] data, int width, int height) throws IOException {
BufferedImage buffered_oldImage = ImageIO.read(new ByteArrayInputStream(data));
int imageOldWidth = buffered_oldImage.getWidth();
int imageOldHeight = buffered_oldImage.getHeight();
double scale_x = (double) width / imageOldWidth;
double scale_y = (double) height / imageOldHeight;
double scale_xy = Math.min(scale_x, scale_y);
int imageNewWidth = (int) (imageOldWidth * scale_xy);
int imageNewHeight = (int) (imageOldHeight * scale_xy);
BufferedImage buffered_newImage = new BufferedImage(imageNewWidth, imageNewHeight, BufferedImage.TYPE_INT_RGB);
buffered_newImage.getGraphics().drawImage(buffered_oldImage.getScaledInstance(imageNewWidth, imageNewHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
buffered_newImage.getGraphics().dispose();
ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
ImageIO.write(buffered_newImage, "jpeg", outPutStream);
return outPutStream.toByteArray();
}
文件上傳 圖片查看
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門浙炼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來份氧,“玉大人,你說我怎么就攤上這事弯屈∥现模” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵资厉,是天一觀的道長厅缺。 經(jīng)常有香客問我,道長酌住,這世上最難降的妖魔是什么店归? 我笑而不...
- 正文 為了忘掉前任阎抒,我火速辦了婚禮酪我,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘且叁。我一直安慰自己都哭,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布逞带。 她就那樣靜靜地躺著欺矫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪展氓。 梳的紋絲不亂的頭發(fā)上穆趴,一...
- 文/蒼蘭香墨 我猛地睜開眼歪赢,長吁一口氣:“原來是場噩夢啊……” “哼化戳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起埋凯,我...
- 序言:老撾萬榮一對情侶失蹤点楼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后白对,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盟步,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年躏结,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了却盘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狰域。...
- 正文 年R本政府宣布抬探,位于F島的核電站,受9級特大地震影響帆赢,放射性物質(zhì)發(fā)生泄漏小压。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一椰于、第九天 我趴在偏房一處隱蔽的房頂上張望怠益。 院中可真熱鬧,春花似錦瘾婿、人聲如沸蜻牢。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽抢呆。三九已至,卻和暖如春笛谦,著一層夾襖步出監(jiān)牢的瞬間抱虐,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓好啰,卻偏偏與公主長得像轩娶,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子框往,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- TCP傳輸 TCP傳輸是怎樣建立的呢贤重? Socket和ServerSocket 建立客戶端和服務(wù)器端 建立連接后,...
- 由于項(xiàng)目還在開發(fā)階段祭犯,只能貼上部分代碼,敬請諒解滚停; 注意點(diǎn): 在后臺上傳必須是調(diào)用此方法:uploadTaskWi...
- 1.項(xiàng)目中需要圖片的異步上傳,度娘之后發(fā)現(xiàn)有一個(gè)插件可以完成這個(gè)需求,即是:Uploadify 下面是這個(gè)異步上傳...
- 用手沖壺沖一杯醇正的單品咖啡并不難沃粗。工具容易準(zhǔn)備,過程簡單键畴∽钪眩花三五分鐘的時(shí)間,在家或者辦公室就可享用一杯香醇咖啡起惕,...