java web开发,上传图片并读取

java web开发,上传图片并读取,第1张

java web开发中,使用文件 *** 作类来上传并读取,如下代码:

  @desc: 处理工具

  @author: bingye

  @createTime: 2015-3-17 下午04:25:32

  @version: v10

 /

public class ImageUtil {

 

    /

     将写到客户端

     @author: bingye

     @createTime: 2015-3-17 下午04:36:04

     @history:

     @param image

     @param response void

    /

    public static void writeImage(byte[] image,>

FileInputStreamhFile=newFileInputStream(url);   //得到文件大小

inti=hFileavailable();

bytedata[]=newbyte[i];    //读数据

hFileread(data);     //得到向客户端输出二进制数据的对象

OutputStreamtoClient=responsegetOutputStream();     //输出数据

toClientwrite(data);

toClientflush();

toClientclose();

hFileclose();

扩展资料

如果是纯文本使用字符流,如果二进制文件,使用字节流。

如果只是得到信息,原样不动,不进行修改 *** 作,例如文件上传和下载,这时就使用字节流。文件上传:在服务器端把浏览器端信息提取出来。文件下载:把服务器端内容写给浏览器端。

如果要 *** 作的是自定义信息,这时使用字符流。

通过response获取的输出流它的真实类型是什么?

ServletOutputStream responsegetOutputStream();

PrintWriter responsegetWriter();

ServletOutputStream由于使用字节流多数是原样复制,所以使用write方法,而不是print方法。

PrintWriter:打印流,两个特点:1可以设置自动刷新。2可以将信息原样输出。

DivImageByMouse是个JPanel子类,把main去掉,add到别的容器下也可以正常使用,是按照鼠标移动取的,要按200200的取要稍微改动一下

import javaawtColor;

import javaawtGraphics;

import javaawtImage;

import javaawtToolkit;

import javaawtdatatransferDataFlavor;

import javaawtdatatransferTransferable;

import javaawtdatatransferUnsupportedFlavorException;

import javaawteventMouseAdapter;

import javaawteventMouseEvent;

import javaawteventMouseMotionAdapter;

import javaawtimageBufferedImage;

import javaioFile;

import javaioIOException;

import javaximageioImageIO;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJOptionPane;

import javaxswingJPanel;

public class DivImageByMouse extends JPanel{

BufferedImage image=null;

int x1,y1,x2,y2;

public DivImageByMouse(File file){

super();

thisaddMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e) {

x1=egetX();

y1=egetY();

}

public void mouseReleased(MouseEvent e) {

x2=egetX();

y2=egetY();

int x=x1<x2x1:x2;

int y=y1<y2y1:y2;

int w=(x1>x2x1:x2)-x;

int h=(y1>y2y1:y2)-y;

Image image=DivImageByMousethisgetImageByClip(x, y, w, h);

setClipboardImage2(image);

x1=y1=x2=y2=0;

JOptionPaneshowMessageDialog(DivImageByMousethis,"已保存到系统粘贴板!","已保存",JOptionPaneINFORMATION_MESSAGE);

DivImageByMousethisrepaint();

}

});

thisaddMouseMotionListener(new MouseMotionAdapter(){

public void mouseDragged(MouseEvent e) {

x2=egetX();

y2=egetY();

DivImageByMousethisrepaint();

}

});

try {

image=ImageIOread(file);

} catch (IOException e) {

Systemoutprintln("输入文件不是一个文件!");

}

}

public Image getImage(){

return image;

}

public Image getImageByClip(int x,int y,int w,int h){

int rgbs[]=new int[wh];

rgbs=imagegetRGB(x,y, w, h, rgbs,0,w);

BufferedImage tmpImage=new BufferedImage(w,h,BufferedImageTYPE_INT_ARGB);

tmpImagesetRGB(0, 0, w, h, rgbs,0,w);

return tmpImage;

}

public void paint(Graphics g){

superpaint(g);

gdrawImage(image,0,0,this);

Systemoutprintln("("+x1+","+y1+")("+x2+","+y2+")");

if(x1==0&&y1==0&&x2==0&&y2==0) return;

Systemoutprintln("rect");

int x=x1<x2x1:x2;

int y=y1<y2y1:y2;

int w=(x1>x2x1:x2)-x;

int h=(y1>y2y1:y2)-y;

gsetColor(Colorblue);

gdrawRect(x, y, w, h);

}

protected static void setClipboardImage2(final Image image) {

Transferable trans = new Transferable(){

public DataFlavor[] getTransferDataFlavors() {

return new DataFlavor[] { DataFlavorimageFlavor };

}

public boolean isDataFlavorSupported(DataFlavor flavor) {

return DataFlavorimageFlavorequals(flavor);

}

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

if(isDataFlavorSupported(flavor))

return image;

throw new UnsupportedFlavorException(flavor);

}

};

ToolkitgetDefaultToolkit()getSystemClipboard()setContents(trans, null);

}

public static void main(String args[]){

JFrame jf=new JFrame("");

jfsetDefaultCloseOperation(jfEXIT_ON_CLOSE);

jfsetBounds(50,50,1024,768);

jfadd(new DivImageByMouse(new File("e:\\109jpg")));

jfadd(new JButton(""),"North");

jfsetVisible(true);

}

}

用第三方工具去取 common-upload,具体取到的方法参考代码如下:

FileItemFactory fileItemFactory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(fileItemFactory);

uploadsetHeaderEncoding("utf-8");

try {

List<FileItem> items = uploadparseRequest(request);

for (FileItem fileItem : items) {

Systemoutprintln("fileName=" + fileItemgetFieldName());

//获取文件流

InputStream in = fileItemgetInputStream();

ServletContext context = getServletConfig()getServletContext();

String path = contextgetRealPath("image");

Systemoutprintln(path);

OutputStream out = new FileOutputStream(new File(path + "\\" + fileItemgetName()));

byte[] buffer = new byte[1024];

int len = 0;

while((len = inread(buffer)) != -1) {

outwrite(buffer, 0, len);

}

outclose();

inclose();

Systemoutprintln("写入完毕");

}

} catch (FileUploadException e) {

eprintStackTrace();

}

Servlet:

String path = "D:\\pictures\\";//换成自己的

File folder = new File(path);

File temp[] = folderlistFiles();

String[] picNames = new String[templength];

for(int i=0;i<templength;i++) {

picNames[i] = temp[i]getName();

}

requestsetAttribute("picNames", picNames);

JSP:requestgetAttribute("picNames");

<%for() {%>

<img src="<%=picNames[i]%>">

<%}%>

以上就是关于java web开发,上传图片并读取全部的内容,包括:java web开发,上传图片并读取、java web二进制流的图片如何用response返回给前台、java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9513101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存