把ss("yingzi"+tm+".png",jg)改成ss("%yingzi"+tm+".png",jg)或者ss("%yingzi/"+tm+".png",jg)
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)商南免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
第二個(gè)是保存在“yingzi”文件夾里的
我自己做過(guò)一個(gè)類似的電子相冊(cè),但功能很不夠全面,給你參考下...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class img4 extends Applet
{
static Button btn1,btn2,btn3,btn4,btn5;
public void init()
{
setBackground(Color.cyan);
setVisible(true);
setLayout(null);
img4cvs cvs=new img4cvs();
btn1=new Button("下一頁(yè)");
btn2=new Button("上一頁(yè)");
btn4=new Button("自動(dòng)播放");
btn5=new Button("停止");
add(btn1);
add(btn2);
add(btn4);
add(btn5);
add(cvs);
btn2.setBounds(550,60,60,30);
btn1.setBounds(550,120,60,30);
btn4.setBounds(550,180,60,30);
btn5.setBounds(550,240,60,30);
cvs.setBounds(30,30,500,400);
btn1.addActionListener(cvs);
btn2.addActionListener(cvs);
btn4.addActionListener(cvs);
btn5.addActionListener(cvs);
validate();
}
class Backcolor extends Thread
{
public void run()
{int j=0;int m=0;int n=0;
while(true)
{
try{
if(j255m255n255)
{
j++;
sleep(100);
Color col1=new Color(j,m,n);
setBackground(col1); }
else if(j==255m255)
{
m++;
sleep(100);
Color col2=new Color(j,m,n);
setBackground(col2); }
else if(j==255m==255n255)
{
n++;
sleep(100);
Color col3=new Color(j,m,n);
setBackground(col3);}
else if(j==255m==255n==255)
{
j=55;
m=55;
n=55;
}
}
catch(InterruptedException e){}
}
}
}
public void start()
{
Backcolor thread2=new Backcolor();
thread2.start();
}
}
class img4cvs extends Canvas implements ActionListener
{
Image[] img;
int pg=0;
volatile boolean pleaseStop;
//String FONTS="Serif";
//String TEXT="我們很性感2!!";
img4cvs()
{
img=new Image[12];
Toolkit tl=getToolkit();
for(int i=0;i=11;i++)
{
img[i]=tl.getImage("img0"+i+".JPG");
}
}
class Play extends Thread
{
public void run()
{
while(!pleaseStop)
{
try{
pg++;
if(pg11){pg=0;}
sleep(1500);
}
catch(InterruptedException e){}
repaint();
}
}
}
public void actionPerformed(ActionEvent e)
{
Play thread=new Play();
if(e.getSource()==img4.btn1)
{
pg++;
if(pg11){pg=0;}
repaint();
}
else if(e.getSource()==img4.btn2)
{
pg--;
if(pg0){pg=11;}
repaint();
}
else if(e.getSource()==img4.btn4)
{
pleaseStop=false;
thread.start();
}
else if(e.getSource()==img4.btn5)
{
pleaseStop=true;
}
}
public void paint(Graphics g)
{
/*g.setColor(Color.black);
g.setFont(new Font(FONTS,Font.BOLD+Font.ITALIC,20));
g.drawString(TEXT,0,500);*/
g.drawImage(img[pg],0,0,500,400,this);
}
}
/*
applet code="img4.class" width=650 height=500
/applet
*/
先初始化;
注意自定義 的
private final String IMAGE_TYPE = "image/*";private final int IMAGE_CODE = 0;
private void setImage() {
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); ? ?getAlbum.setType(IMAGE_TYPE); ? ?startActivityForResult(getAlbum, IMAGE_CODE);}
這里使用Intent獲得相冊(cè)圖片數(shù)據(jù),并返回原來(lái)界面;
這里說(shuō)明一下
startActivityForResult(getAlbum, IMAGE_CODE);
補(bǔ)充{
startActivityForResult與startActivity的不同之處在于:
1、startActivity( )?
僅僅是跳轉(zhuǎn)到目標(biāo)頁(yè)面,若是想跳回當(dāng)前頁(yè)面,則必須再使用一次startActivity( )。
2、startActivityForResult( )?
可以一次性完成這項(xiàng)任務(wù),當(dāng)程序執(zhí)行到這段代碼的時(shí)候,假若從T1Activity跳轉(zhuǎn)到下一個(gè)Text2Activity,而當(dāng)這個(gè)Text2Activity調(diào)用了finish()方法以后,程序會(huì)自動(dòng)跳轉(zhuǎn)回T1Activity,并調(diào)用前一個(gè)T1Activity中的onActivityResult( )方法。
}
startActivityForResult(getAlbum, IMAGE_CODE);
這里采用startActivityForResult來(lái)做跳轉(zhuǎn),此處的IMAGE_CODE實(shí)質(zhì)是0為一個(gè)依據(jù),可以寫其他的值,但一定要=0
然后重寫onActivityResult用于接收剛剛得到的數(shù)據(jù)
protected ?void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bm = null; ? ?ContentResolver resolver = getContentResolver(); ? ?if (requestCode == IMAGE_CODE) { ? ? ? ?try {
Uri originalUri = data.getData(); ? ? ? ? ? ?bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); ? ? ? ? ? ?imgShow.setImageBitmap(bm); ? ? ? ?} catch (IOException e) {
e.printStackTrace(); ? ? ? ?}
}
補(bǔ)充{
ContentProvider-----數(shù)據(jù)提供給第三方應(yīng)用程序使用時(shí)使用
因?yàn)樵贏ndroid系統(tǒng)里面,數(shù)據(jù)庫(kù)是私有的。一般情況下外部應(yīng)用程序是沒(méi)有權(quán)限讀取其他應(yīng)用程序的數(shù)據(jù)。如果你想公開你自己的數(shù)據(jù),你有兩個(gè)選擇:你可以創(chuàng)建你自己的內(nèi)容提供器(一個(gè)ContentProvider子類)或者你可以給已有的提供器添加數(shù)據(jù)-如果存在一個(gè)控制同樣類型數(shù)據(jù)的內(nèi)容提供器且你擁有寫的權(quán)限
}
這里是創(chuàng)建一個(gè)新的內(nèi)容提供器,然后在Activity當(dāng)中通過(guò)getContentResolver()可以得到當(dāng)前應(yīng)用的 ContentResolver實(shí)例
if (requestCode == IMAGE_CODE) {
resultCode為回傳的標(biāo)記這句話是在確認(rèn)是否回傳的是你所需要的;如果是就執(zhí)行try;不然就執(zhí)行catch (IOException e) {
e.printStackTrace();}先說(shuō)這里當(dāng)try語(yǔ)句中出現(xiàn)異常是時(shí),會(huì)執(zhí)行catch中的語(yǔ)句,IOException e也就是實(shí)例化Exception類型的對(duì)象。e是此對(duì)象引用名稱。然后e(引用)會(huì)自動(dòng)調(diào)用Exception類中指定的方法,也就出現(xiàn)了e.printStackTrace() ;。printStackTrace()方法的意思是:在命令行打印異常信息在程序中出錯(cuò)的位置及原因。
try {
Uri originalUri = data.getData(); ? ?bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); ? ?imgShow.setImageBitmap(bm);接收數(shù)據(jù)處理數(shù)據(jù)并顯示照片。
然后進(jìn)行測(cè)試。
另附源碼:
MainActivity:
package com.example.administrator.myapplication1;import android.graphics.BitmapFactory;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import java.io.IOException;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.app.Activity;import android.content.ContentResolver;import android.content.Intent;import android.graphics.Bitmap;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { ? ?private final String IMAGE_TYPE = "image/*"; ? ?private final int IMAGE_CODE = 0; ? ?private Button choose=null; ? ?private ImageView imgShow; ? ?@Override ? ?protected void onCreate(Bundle savedInstanceState) { ? ? ? ?super.onCreate(savedInstanceState); ? ? ? ?setContentView(R.layout.activity_main); ? ? ? ?init();} ? ?private void init() { ? ? ? choose=(Button) findViewById(R.id.button1); ? ? ? ?imgShow=(ImageView) findViewById(R.id.imgShow); ? ? ? ?choose.setOnClickListener(listener); ? ?} ? ?private OnClickListener listener=new OnClickListener(){ ? ? ? ?@Override ? ? ? ?public void onClick(View v) {
setImage(); ? ? ? ? ? ?}
}; ? ? ? ?private void setImage() {
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); ? ? ? ? ? ?getAlbum.setType(IMAGE_TYPE); ? ? ? ? ? ?startActivityForResult(getAlbum, IMAGE_CODE); ? ? ? ?} ? ?protected ?void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bm = null; ? ? ? ?ContentResolver resolver = getContentResolver(); ? ? ? ?if (requestCode == IMAGE_CODE) { ? ? ? ? ? ?try {
Uri originalUri = data.getData(); ? ? ? ? ? ? ? ?bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); ? ? ? ? ? ? ? ?imgShow.setImageBitmap(bm); ? ? ? ? ? ?} catch (IOException e) {
e.printStackTrace(); ? ? ? ? ? ?}
}
}
}
layout:activity_main.xml:
?xml version="1.0" encoding="utf-8"?LinearLayout xmlns:android="" ? ?android:layout_width="match_parent" ? ?android:layout_height="match_parent" ? ?android:orientation="vertical" ? ? ? ?Button ? ? ? ? ? ?android:id="@+id/button1" ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="選擇照相" ? ? ? ? ? ?android:layout_marginRight="190dp" ? ? ? ? ? ?android:layout_marginTop="8dp" ? ? ? ? ? ?android:paddingBottom="2dp" ? ? ? ? ? ?android:background="#aabbcc" / ? ?ImageView ? ? ? ?android:id="@+id/imgShow" ? ? ? ?android:layout_width="fill_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:layout_margin="15dip" ? ? ? ?//LinearLayout
提交頁(yè)面表單
form action="up.jsp" enctype="multipart/form-data" method="post"
input type="file" name="file"
input type="submit" value="確定"
/form
上傳頁(yè)面up.jsp
%@page import="java.io.FileWriter"%
%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.io.*"
pageEncoding="UTF-8"%
%
/**
協(xié)議頭四行內(nèi)容
45 -----------------------------7de231211204c4
80 Content-Disposition: form-data; name="file"; filename="xx.txt"
26 Content-Type: text/plain
2
標(biāo)記文件結(jié)尾
-----------------------------7de231211204c4--
**/
ServletInputStream sin = request.getInputStream();
byte[] buffer = new byte[1024 * 8];
int length = 0, row = 0;
String mark = "";
String filename = "";
while ((length = sin.readLine(buffer, 0, buffer.length)) 0) {
out.println(length + " " + new String(buffer, 0, length, "UTF-8") + "br");
String s = new String(buffer, 0, length, "UTF-8");
if (row == 0)
mark = s.trim();
else if (s.indexOf("filename=") 0) {
int end = s.lastIndexOf("\"");
int start = s.substring(0, end).lastIndexOf("\"");
filename = s.substring(start + 1, end);
} else if ("".equals(s.trim()))
break;
row ++;
}
out.println("filename: " + filename + "br");
filename = request.getRealPath("/") + "../" + filename;
FileOutputStream fout = new FileOutputStream(filename);
while ((length = sin.readLine(buffer, 0, buffer.length)) 0) {
String s = new String(buffer, 0, length);
if (s.startsWith(mark))
break;
fout.write(buffer, 0, length);
}
fout.flush();
fout.close();
File f = new File(filename);
out.println(f.exists());
out.println(f.getAbsolutePath());
%
我這有幾套基于javaweb的電子相冊(cè)源碼, 基礎(chǔ)功能都有實(shí)現(xiàn)
項(xiàng)目基于springmvc+spring+mybatis進(jìn)行實(shí)現(xiàn), BS架構(gòu), MVC設(shè)計(jì)模式分層實(shí)現(xiàn), 數(shù)據(jù)庫(kù)采用mysql 還是比較簡(jiǎn)單的
java web開發(fā)中,使用文件操作類來(lái)上傳圖片并讀取,如下代碼:
*?@desc:?圖片處理工具
*?@author:?bingye
*?@createTime:?2015-3-17?下午04:25:32
*?@version:?v1.0
*/
public?class?ImageUtil?{
/**
*?將圖片寫到客戶端
*?@author:?bingye
*?@createTime:?2015-3-17?下午04:36:04
*?@history:
*?@param?image
*?@param?response?void
*/
public?static?void?writeImage(byte[]?image,HttpServletResponse?response){
if(image==null){
return;
}
byte[]?buffer=new?byte[1024];
InputStream?is=null;
OutputStream?os=null;
try?{
is=new?ByteArrayInputStream(image);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally{
try?{
if(is!=null){is.close();}
if(os!=null){os.close();}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
/**
*?獲取指定路勁圖片
*?@author:?bingye
*?@createTime:?2015-3-21?上午10:50:44
*?@param?filePath
*?@param?response?void
*/
public?static?void?writeImage(String?filePath,HttpServletResponse?response){
File?imageFile=new?File(filePath);?
if(imageFile!=null??imageFile.exists()){
byte[]?buffer=new?byte[1024];
InputStream?is=null;
OutputStream?os=null;
try?{
is=new?FileInputStream(imageFile);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally{
try?{
if(is!=null){is.close();}
if(os!=null){os.close();}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
/**
*?圖片上傳到文件夾
*?@author:?bingye
*?@createTime:?2015-3-20?下午08:07:25
*?@param?file
*?@param?savePath
*?@return?boolean
*/
public?static?ResultDto?uploadToLocal(CommonsMultipartFile?file,String?savePath){
if(file!=null??!file.isEmpty()){
//獲取文件名稱
String?fileName=file.getOriginalFilename();
//獲取后綴名
String?suffixName=fileName.substring(fileName.indexOf(".")+1);
//新名稱
String?newFileName=System.currentTimeMillis()+"."+suffixName;
//新文件路勁
String?filePath=savePath+newFileName;
//獲取存儲(chǔ)文件路徑
File?fileDir=new?File(savePath);
if(!fileDir.exists()){
//如果文件夾沒(méi)有:新建
fileDir.mkdirs();
}
FileOutputStream?fos=null;
try?{
fos=new?FileOutputStream(filePath);
fos.write(file.getBytes());
fos.flush();
return?ResultUtil.success("UPLOAD_SUCCESS",?URLEncoder.encode(newFileName,"utf-8"));
}?catch?(Exception?e)?{
e.printStackTrace();
return?ResultUtil.fail("UPLOAD_ERROR");
}?finally{
try?{
if(fos!=null){
fos.close();
}
}?catch?(IOException?e)?{
e.printStackTrace();
return?ResultUtil.fail("UPLOAD_ERROR");
}
}
}
return?ResultUtil.fail("UPLOAD_ERROR");
}
}
新聞標(biāo)題:java網(wǎng)頁(yè)相冊(cè)代碼 html相冊(cè)的源代碼
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article8/hiceip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(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)