①.、打开电脑,进入软件中,编写设置大小的代码.
页面里你用uploadify或jquery之类的随意.
后台spring配置文件applicationContext.xml中配置
bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
/bean
controller的方法定义类似于,如果不一定需要上传文件的话,最好再加上if(!logo.isEmpty())的判断
@RequestMapping(value="/upload")
public?ModelAndView?editSoftware(HttpServletRequest?request,@RequestParam(required=false)?MultipartFile?logo){
ModelAndView?view=new?ModelAndView();
//定义上传路径
String?path=request.getSession().getServletContext().getRealPath("/resources/upload/");
//获取上传文件的文件名
String?fileName=logo.getOriginalFilename();
File?file=new?File(path,?fileName);
try?{
//保存文件
FileUtils.copyInputStreamToFile(logo.getInputStream(),?file);
}?catch?(IOException?e)?{
e.printStackTrace();
}
view.setViewName("");
return?view;
图片名字是img1.JPG,你放到本程序的目录下面就好了.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class TestMenu1 extends JFrame{
private JTextArea textArea=new JTextArea();
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("文件");
private JMenu viewMenu=new JMenu("视图");
private JMenu toolMenu=new JMenu("工具栏");
private JMenuItem[] fileItem={new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")};
private JMenuItem[] viewItem={new JMenuItem("普通"),new JMenuItem("页面")};
private JCheckBoxMenuItem[] toolItem={new JCheckBoxMenuItem("常用"),new JCheckBoxMenuItem("绘图"),new JCheckBoxMenuItem("符号栏")};
private JPanel jPanel1;
private JLabel jLabel;
private Image image;
private ImageIcon imageIcon ;
public TestMenu1(String title){
super(title);
jPanel1=new JPanel();
image = Toolkit.getDefaultToolkit().getImage("img1.JPG");
jLabel = new JLabel();
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
add(jLabel, BorderLayout.NORTH);
add(jPanel1, BorderLayout.SOUTH);
setVisible(true);
ActionListener actListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(((JMenuItem)e.getSource()).getText()=="退出"){
System.exit(0);
}else{
textArea.setText(((JMenuItem)e.getSource()).getText());
};
ItemListener itemListener=new ItemListener(){
public void itemStateChanged(ItemEvent e){
String str=new String("");
for(int i=0;itoolItem.length;i◆◆){
if(toolItem[i].isSelected()){
str◆=toolItem[i].getText()◆"\n";
if(str.length()==0){
textArea.setText("没有复选");
textArea.setText(str);
for(int i=0;ifileItem.length;i◆◆){
fileItem[i].addActionListener(actListener);
fileMenu.add(fileItem[i]);
fileMenu.addSeparator();
for(int i=0;iviewItem.length;i◆◆){
viewItem[i].addActionListener(actListener);
viewMenu.add(viewItem[i]);
viewMenu.addSeparator();
viewMenu.add(toolMenu);
toolItem[i].addItemListener(itemListener);
toolMenu.add(toolItem[i]);
menuBar.add(fileMenu);
menuBar.add(viewMenu);
setJMenuBar(menuBar);
add(new JScrollPane(textArea));
public static void main(String[] args){
//TestMenu frame=
new TestMenu1("DEMO");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setVisible(true);
("String")
上面两个都是一样的字符串的意思,只要new ImageIcon("")不报错就没有问题,不要去纠结加不加引号的问题
①.)文件要有后缀名
以上就是土嘎嘎小编为大家整理的java代码如何传入图片相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!