java添加图片代码片

二次元同好交流新大陆
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1581)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_087074',
blogTitle:'两种方式向Excel中插入图片',
blogAbstract:'
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}Java上传图片到服务器j_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Java上传图片到服务器j
上传于||文档简介
&&J​a​v​a​上​传​图​片​到​服​务​器​j
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
你可能喜欢java上传图片,对图片进行等比例缩放,及局部裁剪的工具类代码 - 为程序员服务
为程序员服务
java上传图片,对图片进行等比例缩放,及局部裁剪的工具类代码
从项目中摘出来的,。
[Java]代码
import java.awt.C
import java.awt.G
import java.awt.Graphics2D;
import java.awt.I
import java.awt.MediaT
import java.awt.R
import java.awt.RenderingH
import java.awt.T
import java.awt.image.BufferedI
import java.awt.image.CropImageF
import java.awt.image.FilteredImageS
import java.awt.image.ImageF
import java.io.BufferedInputS
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.util.UUID;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGC
import com.sun.image.codec.jpeg.JPEGEncodeP
import com.sun.image.codec.jpeg.JPEGImageE
public class FileUploadUtils {
* 裁剪图片
* @param input
* @param basepath
* @param uid
* @param x
* @param y
* @param width
* @param height
* @return 绝对路径
* @throws IOException
public static String cutImg(String input,String basepath,int x,int y,int width,int height) throws IOException{
String path2 = basepath+&/&+ConstantUtils.USERFACETEMPPATH;
String postfix = getPostfix(input);
String dst = path2 +&/&+UUID.randomUUID().toString()+&.&+
createDir(path2);
imgCut(basepath+input,dst,postfix,x,y,width,height);
* 裁剪图片
* @param input
* @param src
* @param x
* @param y
* @param width
* @param height
* @throws IOException
public static void imgCut(String input,String dst,String type,int x,int y,int width,int height) throws IOException
BufferedImage fromimg = ImageIO.read(new File(input));
ImageFilter cropFilter = new CropImageFilter(x,y,width,height);
Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(fromimg.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制小图
g.dispose();
// 输出为文件
// dir = &d:\\temp\\cut_image_& + i + &_& + j + &.jpg&;
File f = new File(dst);
ImageIO.write(tag,type, f);
* 上传头像文件
* @param src
* @param basepath
* @param filename
* @throws Exception
public static String uploadImg(File src,String basepath,String filename) throws Exception{
String daypath = DateTools.getYear() + && + DateTools.getMonth() + && + DateTools.getMonthWeek();
String temppath = ConstantUtils.BASEUPLOADPATH+&/&+ConstantUtils.ORIGINALIMGPATH+&/& +
String thumbnailpath = ConstantUtils.BASEUPLOADPATH+&/&+ConstantUtils.THUMBNAILIMGPATH+&/& +
String postfix = getPostfix(filename);
String uuid = UUID.randomUUID().toString();
String dsttempname = uuid+&.&+
createDir(basepath +&/&+temppath);
createDir (basepath +&/&+ thumbnailpath);
String dstallpath = basepath +&/&+temppath+&/&+
String dstthumbnailpath = basepath +&/&+thumbnailpath+&/&+
copy(src,new File(dstallpath));
//对上传的文件进行 等比例 裁剪。
按照前段要展现的
height width
Thumbnail(dstallpath,dstthumbnailpath,350,300,100);
//返回裁剪后的路径
return thumbnailpath+&/&+
* 上传文件
* @param src
* @param dst
* @throws Exception
public static void copy(File src, File dst) throws Exception {
InputStream in =
OutputStream out =
in = new BufferedInputStream(new FileInputStream(src), ConstantUtils.BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst), ConstantUtils.BUFFER_SIZE);
byte[] buffer = new byte[ConstantUtils.BUFFER_SIZE];
while (in.read(buffer) & 0) {
out.write(buffer);
} finally {
if (null != in) {
in.close();
if (null != out) {
out.close();
} catch (Exception e) {
e.printStackTrace();
* 得到文件后缀
* @param fileName
public static String getPostfix(String fileName){
if (fileName.equals(&&))
return &&;
int pos = fileName.lastIndexOf(&.&);
if (pos & 0) {
return fileName.substring(fileName.length() - 3).toLowerCase();
return fileName.substring(pos+1).toLowerCase();
* 创建目录
* @param filePath
public static void createDir(String filePath) {
File myFile = new File(filePath);
if (!myFile.exists()) {
if (!myFile.mkdirs())
System.out.println(&创建目录 fail&);
System.out.println(&创建目录 success&);
* 等比例缩放图片
* @param infile
* @param outfile
* @param width
* @param height
* @param quality
* @throws IOException
* @throws InterruptedException
public static void Thumbnail(String infile, String outfile, int width, int height, int quality) throws IOException, InterruptedException {
// save thumbnail image to OUTFILE
//System.out.println(&infile:& + infile);
BufferedImage thumbImage =
BufferedOutputStream out =
Image image =
image = Toolkit.getDefaultToolkit().createImage(infile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
int thumbWidth =
int thumbHeight =
double thumbRatio = (double) thumbWidth / (double) thumbH
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageH
if (thumbRatio & imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
thumbWidth = (int) (thumbHeight * imageRatio);
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image,0,0, thumbWidth, thumbHeight, null);
out = new BufferedOutputStream(new FileOutputStream(outfile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
thumbImage =
您可能的代码
相关聚客文章
荣誉:1115
相关专栏文章  整理后可执行代码如下:
  import java.awt.*;
  import javax.swing.*;
  public class TestBackgroundColor extends JFrame
  public static void main(String[] args)
  // TODO Auto-generated method stub
  TestBackgroundColor tbc = new TestBackgroundColor();
  tbc.setVisible(true);
  private JPanel imageP
  private ImageI
  public TestBackgroundColor()
  background = new ImageIcon("渐变背景14.png");//背景图片
  JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
  //把标签的大小位置设置为图片刚好填充整个面板
  label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
  //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
  imagePanel = (JPanel)this.getContentPane();
  imagePanel.setOpaque(false);
  //内容窗格默认的布局管理器为BorderLayout
  imagePanel.setLayout(new FlowLayout());
  imagePanel.add(new JButton("测试按钮"));
  this.getLayeredPane().setLayout(null);
  //把背景图片添加到分层窗格的最底层作为背景
  this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(background.getIconWidth(),background.getIconHeight());
  this.setVisible(true);
  效果图如下:
【】【】【】【】
  《全国专业技术人员计算机应用能力考试标准教程―word&2003中文字处理》严格根据..
定价:¥39.50
优惠价:¥29.60&&本书适合报考全国专业技术人员计算机应用能力考试“中文windows&xp操作系统”科目的..
定价:¥39.50
优惠价:¥29.60&&
????????????
????????????
         Copyright ©
() All Rights Reserved查看:4542|回复:2
package com.mwq.
import java.awt.D
import java.awt.FlowL
import java.awt.F
import java.awt.GridBagC
import java.awt.GridBagL
import java.awt.I
import java.awt.event.ActionE
import java.awt.event.ActionL
import java.awt.event.ItemE
import java.awt.event.ItemL
import java.util.I
import javax.swing.ImageI
import javax.swing.JB
import javax.swing.JComboB
import javax.swing.JF
import javax.swing.JL
import javax.swing.JOptionP
import javax.swing.JP
import javax.swing.JPasswordF
import javax.swing.SwingC
import javax.swing.border.TitledB
import com.mwq.hibernate.D
import com.mwq.hibernate.mapping.TbM
import com.mwq.hibernate.mapping.TbR
public class LandFrame extends JFrame {
private JPasswordField passwordF
private JComboBox userNumComboB
&&* Launch the application
&&* @param args
public static void main(String args[]) {
& &LandFrame frame = new LandFrame();
& &frame.setVisible(true);
&&} catch (Exception e) {
& &e.printStackTrace();
&&* Create the frame
public LandFrame() {
&&super();
&&getContentPane().setLayout(new GridBagLayout());
&&setTitle(& 企业&);
&&setResizable(false);
&&setBounds(100, 100, 400, 300);
&&setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
&&dao = Dao.getInstance();
&&final JLabel softTitleLabel = new JLabel();
&&softTitleLabel.setFont(new Font(&&, Font.BOLD, 22));
&&softTitleLabel.setBorder(new TitledBorder(null, &&,
& & TitledBorder.DEFAULT_JUSTIFICATION,
& & TitledBorder.DEFAULT_POSITION, null, null));
&&softTitleLabel.setText(&企业人力资源管理系统&);
&&final GridBagConstraints gridBagConstraints = new GridBagConstraints();
&&gridBagConstraints.insets = new Insets(0, 0, 20, 0);
&&gridBagConstraints.gridy = 0;
&&gridBagConstraints.gridx = 0;
&&getContentPane().add(softTitleLabel, gridBagConstraints);
&&final JPanel userNumPanel = new JPanel();
&&userNumPanel.setLayout(new FlowLayout());
&&final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
&&gridBagConstraints_1.gridy = 1;
&&gridBagConstraints_1.gridx = 0;
&&getContentPane().add(userNumPanel, gridBagConstraints_1);
&&final JLabel userNumLabel = new JLabel();
&&userNumLabel.setVerticalTextPosition(SwingConstants.TOP);
&&userNumLabel.setText(&管理员:&);
&&userNumPanel.add(userNumLabel);
&&userNumComboBox = new JComboBox();
&&userNumComboBox.addItemListener(new ItemListener() {
& &public void itemStateChanged(ItemEvent arg0) {
& & String username = userNumComboBox.getSelectedItem().toString()
& && &.trim();
& & if (username.equals(&TSoft&)) {
& &&&passwordField.setText(&111&);
&&userNumComboBox.setPreferredSize(new Dimension(100, 20));
&&userNumComboBox.addItem(&&&请 选 择&&&);
&&Iterator managerIt = dao.queryManagerOfNatural().iterator();
&&if (managerIt.hasNext()) {
& &while (managerIt.hasNext()) {
& & TbManager manager = (TbManager) managerIt.next();
& & StringBuffer item = new StringBuffer(&& &&);
& & item.append(manager.getTbRecord().getRecordNumber());
& & userNumComboBox.addItem(item.toString());
&&} else {
& &userNumComboBox.addItem(&& &TSoft&);
&&userNumPanel.add(userNumComboBox);
&&final JPanel passwordPanel = new JPanel();
&&passwordPanel.setLayout(new FlowLayout());
&&final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
&&gridBagConstraints_4.gridy = 2;
&&gridBagConstraints_4.gridx = 0;
&&getContentPane().add(passwordPanel, gridBagConstraints_4);
&&final JLabel passwordLabel = new JLabel();
&&passwordLabel.setText(&密&&码:&);
&&passwordPanel.add(passwordLabel);
&&passwordField = new JPasswordField();
&&passwordField.setPreferredSize(new Dimension(100, 20));
&&passwordPanel.add(passwordField);
&&final JPanel buttonPanel = new JPanel();
&&final FlowLayout flowLayout = new FlowLayout();
&&flowLayout.setAlignment(FlowLayout.RIGHT);
&&buttonPanel.setLayout(flowLayout);
&&final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
&&gridBagConstraints_2.ipadx = 15;
&&gridBagConstraints_2.insets = new Insets(10, 0, 0, 0);
&&gridBagConstraints_2.gridy = 3;
&&gridBagConstraints_2.gridx = 0;
&&getContentPane().add(buttonPanel, gridBagConstraints_2);
&&final JButton landButton = new JButton();
&&landButton.addActionListener(new ActionListener() {
& &public void actionPerformed(ActionEvent e) {
& & String userNum = userNumComboBox.getSelectedItem().toString()
& && &.trim();
& & if (userNum.equals(&请 选 择&)) {
& &&&JOptionPane.showMessageDialog(null, &请选择登录管理员!&, &友情提示&,
& && & RMATION_MESSAGE);
& & char[] passwords = passwordField.getPassword();
& & StringBuffer passwordBuf = new StringBuffer();
& & for (int i = 0; i & passwords. i++) {
& &&&passwordBuf.append(passwords);
& & String password = passwordBuf.toString().trim();
& & if (password.length() == 0) {
& &&&JOptionPane.showMessageDialog(null, &请输入登录密码!&, &友情提示&,
& && & RMATION_MESSAGE);
& & IndexFrame indexFrame =
& & if (userNum.equals(&TSoft&)) {
& &&&if (password.equals(&111&)) {
& && &indexFrame = new IndexFrame(null);
& &&&} else {
& && &JOptionPane.showMessageDialog(null,
& && &&&&默认用户“TSoft”的登录密码为“111”!&, &友情提示&,
& && &&&RMATION_MESSAGE);
& && &passwordField.setText(&111&);
& & } else {
& &&&TbRecord record = (TbRecord) dao.queryRecordByNum(userNum);
& &&&TbManager manager = record.getTbManager();
& &&&if (password.equals(manager.getPassword())) {
& && &indexFrame = new IndexFrame(record);
& &&&} else {
& && &JOptionPane.showMessageDialog(null, &登录密码错误,请确认后重新输入!&,
& && &&&&友情提示&, RMATION_MESSAGE);
& & indexFrame.setVisible(true);
& & setVisible(false);
&&landButton.setMargin(new Insets(2, 6, 2, 6));
&&landButton.setText(&登录&);
&&buttonPanel.add(landButton);
&&final JButton resetButton = new JButton();
&&resetButton.addActionListener(new ActionListener() {
& &public void actionPerformed(ActionEvent e) {
& & userNumComboBox.setSelectedIndex(0);
& & passwordField.setText(null);
&&resetButton.setMargin(new Insets(2, 6, 2, 6));
&&resetButton.setText(&清空&);
&&buttonPanel.add(resetButton);
&&final JButton exitButton = new JButton();
&&exitButton.addActionListener(new ActionListener() {
& &public void actionPerformed(ActionEvent e) {
& & dispose();
&&exitButton.setMargin(new Insets(2, 6, 2, 6));
&&exitButton.setText(&退出&);
&&buttonPanel.add(exitButton);
&&final JLabel defaultLabel = new JLabel();
&&defaultLabel.setText(&默认用户:TSoft& & 默认密码:111&);
&&final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
&&gridBagConstraints_3.insets = new Insets(10, 0, 0, 0);
&&gridBagConstraints_3.gridy = 4;
&&gridBagConstraints_3.gridx = 0;
&&getContentPane().add(defaultLabel, gridBagConstraints_3);
小洋(on my way)
帮顶上去,期待好心人给你解决。
51CTOjava板块讨论QQ群:【1000人群】2群:【可加】 & && && &欢迎热爱JAVA的来讨论^_^
我的个人网站--&
1.JPane中的图片
2.分层显示
具体的我记不得了,网上百度一下,不过swing下的图片显示应该就是这两个方法,我也是刚学java不久,说错的大家指点。

我要回帖

更多关于 java添加图片按钮 的文章

 

随机推荐