1、打開(kāi)電腦,進(jìn)入軟件中,編寫(xiě)設(shè)置大小的代碼。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到青龍網(wǎng)站設(shè)計(jì)與青龍網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋青龍地區(qū)。
2、在左邊的選項(xiàng)中選擇javastudy包,在子文件下選擇Java。
3、選擇之后,就會(huì)看到以下畫(huà)面,通過(guò)代碼加入圖片。
4、寫(xiě)好代碼之后,選擇左邊的菜單。
5、圖片插入之后,就成功了。
不明比你的意思,你是想要將圖片放到j(luò)ava項(xiàng)目中去還是用java寫(xiě)代碼導(dǎo)入圖片。前者:直接復(fù)制粘貼就OK了,后者:你將圖片看成文件就OK了,和文件導(dǎo)入完全相同的,你可以利用Struts2導(dǎo)入
頁(yè)面里你用uploadify或jquery之類的隨意。
后臺(tái)spring配置文件applicationContext.xml中配置
!--?限制上傳文件尺寸,限制為5MB?--
bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
property?name="maxUploadSize"?value="5242880"/
/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("打開(kāi)"),new JMenuItem("保存"),new JMenuItem("退出")};
private JMenuItem[] viewItem={new JMenuItem("普通"),new JMenuItem("頁(yè)面")};
private JCheckBoxMenuItem[] toolItem={new JCheckBoxMenuItem("常用"),new JCheckBoxMenuItem("繪圖"),new JCheckBoxMenuItem("符號(hào)欄")};
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);
setSize(600, 600);
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("沒(méi)有復(fù)選");
}else{
textArea.setText(str);
}
}
};
for(int i=0;ifileItem.length;i++){
fileItem[i].addActionListener(actListener);
fileMenu.add(fileItem[i]);
if(i==2){
fileMenu.addSeparator();
}
}
for(int i=0;iviewItem.length;i++){
viewItem[i].addActionListener(actListener);
viewMenu.add(viewItem[i]);
}
viewMenu.addSeparator();
viewMenu.add(toolMenu);
for(int i=0;itoolItem.length;i++){
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.setSize(300,180);
//frame.setVisible(true);
}
}
("String“)
("pictrue/pic_"+pictureID+"_"+(i*3+j+1)+".jpg");
上面兩個(gè)都是一樣的字符串的意思,只要new ImageIcon("")不報(bào)錯(cuò)就沒(méi)有問(wèn)題,不要去糾結(jié)加不加引號(hào)的問(wèn)題
我看的pic_1_2等都是沒(méi)有后綴的,是否文件名有問(wèn)題,或者在仔細(xì)檢查一下路徑
1)文件要有后綴名
2)要用Image或BufferedImage對(duì)象
3)因?yàn)槟阒貙?xiě)了paint()方法,所以不能在Label里面顯示圖片。你重寫(xiě)了paint()方法后,整個(gè)容器都會(huì)變成畫(huà)布,所以看不到Label組件,自然也就看不到圖片。應(yīng)該在paint方法里面用g.drawImage方法把圖片在畫(huà)布中畫(huà)出來(lái)。參考Java API,Graphics的drawImage方法。
當(dāng)前題目:java代碼如何傳入圖片 java上傳圖片代碼
網(wǎng)站路徑:http://chinadenli.net/article32/hepjpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、商城網(wǎng)站、小程序開(kāi)發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)