新聞中心
java 文件上傳的代碼,盡量詳細一點。。。
// 這是我寫的一個方法,里面只需要傳兩個參數(shù)就OK了,在任何地方調(diào)用此方法都可以文件上傳

專注于為中小企業(yè)提供網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)西華免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
/**
* 上傳文件
* @param file待上傳的文件
* @param storePath待存儲的路徑(該路徑還包括文件名)
*/
public void uploadFormFile(FormFile file,String storePath)throws Exception{
// 開始上傳
InputStream is =null;
OutputStream os =null;
try {
is = file.getInputStream();
os = new FileOutputStream(storePath);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytes);
}
os.close();
is.close();
} catch (Exception e) {
throw e;
}
finally{
if(os!=null){
try{
os.close();
os=null;
}catch(Exception e1){
;
}
}
if(is!=null){
try{
is.close();
is=null;
}catch(Exception e1){
;
}
}
}
}
一個瀏覽上傳多個文件的java代碼
在Java中可以用struts2實現(xiàn)多個文件同時上傳代碼,功能代碼如下:
1、upload.jsp頁面(選擇上傳文件)
form?action="upload.action"?name="uploadForm"?method="post"?enctype="multipart/form-data"
文件標題:input?type="text"?name="title"/br/
選擇文件-:input?type="file"?name="upload"/br/
選擇文件二:input?type="file"?name="upload"/br/
選擇文件三:input?type="file"?name="upload"/br/
input?type="submit"?value="upload"/??
/form
2、action代碼如下:
//對應(yīng)的Action依次遍歷所有文件域,然后生成對應(yīng)的輸入文件流,輸出文件流在指定的服務(wù)器保存路徑中添加對應(yīng)的輸出文件流保存文件。同時動態(tài)指定服務(wù)器上文件的保存路徑。
package?com.inspur.action;??
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;?
import?org.apache.struts2.ServletActionContext;?
import?com.opensymphony.xwork2.ActionSupport;?
public?class?UploadAction?extends?ActionSupport?{
private?String?title;
private?File[]?upload;
private?String[]?uploadFileName;
private?String[]?uploadContentType;
private?String?savePath;?
public?String?getTitle()?{
return?title;
}
public?void?setTitle(String?title)?{
this.title?=?title;
}
public?File[]?getUpload()?{
return?upload;
}
public?void?setUpload(File[]?upload)?{
this.upload?=?upload;
}
public?String[]?getUploadFileName()?{
return?uploadFileName;
}
public?void?setUploadFileName(String[]?uploadFileName)?{
this.uploadFileName?=?uploadFileName;
}
public?String[]?getUploadContentType()?{
return?uploadContentType;
}
public?void?setUploadContentType(String[]?uploadContentType)?{
this.uploadContentType?=?uploadContentType;
}
public?String?getSavePath()?{
return?ServletActionContext.getRequest().getRealPath(savePath);
}
public?void?setSavePath(String?savePath)?{
this.savePath?=?savePath;
}
public?String?upload()throws?Exception{
File[]?files=this.getUpload();
for(int?i=0;ifiles.length;i++){
FileOutputStream?fos=new?FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName()[i]);
byte[]?buffer=new?byte[1024];
FileInputStream?fis=new?FileInputStream(files[i]);
int?len=0;
while((len=fis.read(buffer))0){
fos.write(buffer,0,len);
}
}?
return?SUCCESS;
}?
}
3、success.jsp頁面代碼如下(上傳成功界面顯示所有上傳的圖片)
文件標題:s:property?value="title"/br/
第一個圖片:img?alt="first"?src="s:property?value="'upload/'+uploadFileName[0]"/"/br/
第二個圖片:img?alt="second"?src="s:property?value="'upload/'+uploadFileName[1]"/"/br/
java 商品 系統(tǒng) 代碼
package entity;
public class Market {
private int id;//id
private int num;//數(shù)量
private String goods;//商品
private double price;//價格
public Market(int id, int num, String goods, double price) {
super();
this.id = id;
this.num = num;
this.goods = goods;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getGoods() {
return goods;
}
public void setGoods(String goods) {
this.goods = goods;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double calc( ){
double sum=price*num;
System.out.println("您消費共計:"+sum+"¥");
return sum;
}
}
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import entity.Market;
public class Test {
private static MapInteger,Market goods=new HashMapInteger, Market();
public static void main(String[] args) {
System.out.println("-------超市計價系統(tǒng)-------");
String goods1="可口可樂";
String goods2="爆米花";
String goods3="益達";
printTable("編號","商品","價格");
printTable("1",goods1,"3.0¥");
printTable("2",goods2,"5.0¥");
printTable("3",goods3,"10.0¥");
goods.put(1, new Market(1, 1, goods1, 3.0));
goods.put(2, new Market(2, 1, goods2, 5.0));
goods.put(3, new Market(3, 1, goods3, 10.0));
Scanner input = new Scanner(System.in);
System.out.println("請輸入商品的編號:");
int num = input.nextInt();
System.out.println("請輸入商品的數(shù)量");
int amount = input.nextInt();
Market market = goods.get(num);
market.setNum(amount);
market.calc();
}
private static void printTable(String row1,String row2,String row3 ) {
System.out.print(row1);
int times=12;
if (row2!="商品") {
times=5;
}
for (int i = 0; i times; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i 10; i++) {
System.out.print(" ");
}
System.out.print(row3);
System.out.println("\n");
}
}
//測試結(jié)果:
-------超市計價系統(tǒng)-------
編號 商品 價格
1 可口可樂 3.0¥
2 爆米花 5.0¥
3 益達 10.0¥
請輸入商品的編號:
3
請輸入商品的數(shù)量
5
您消費共計:50.0¥
電腦怎么輸入商品代碼用java程序語言
import java.util.Scanner;
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=100;
Scanner in =new Scanner(System.in);
int n = in.nextInt();
System.out.println(n);
}
}
app怎么上傳圖片到j(luò)ava后臺?java后臺處理的具體代碼是怎么寫的?
使用一些已有的組件幫助我們實現(xiàn)這種上傳功能。常用的上傳組件:Apache的CommonsFileUploadJavaZoom的UploadBeanjspSmartUpload以下,以FileUpload為例講解1、在jsp端要注意enctype="multipart/form-data"然后只需要放置一個file控件,并執(zhí)行submit操作即可2、web端核心代碼如下:publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("UTF-8");DiskFileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploadupload=newServletFileUpload(factory);try{Listitems=upload.parseRequest(request);Iteratoritr=items.iterator();while(itr.hasNext()){FileItemitem=(FileItem)itr.next();if(item.isFormField()){System.out.println("表單參數(shù)名:"+item.getFieldName()+",表單參數(shù)值:"+item.getString("UTF-8"));}else{if(item.getName()!=null!item.getName().equals("")){System.out.println("上傳文件的大小:"+item.getSize());System.out.println("上傳文件的類型:"+item.getContentType());System.out.println("上傳文件的名稱:"+item.getName());FiletempFile=newFile(item.getName());Filefile=newFile(sc.getRealPath("/")+savePath,tempFile.getName());item.write(file);request.setAttribute("upload.message","上傳文件成功!");}else{request.setAttribute("upload.message","沒有選擇上傳文件!");}}}}catch(FileUploadExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();request.setAttribute("upload.message","上傳文件失?。?);}request.getRequestDispatcher("/uploadResult.jsp").forward(request,response);}
java實現(xiàn)文件上傳,代碼盡量簡潔~~~~~·
你說的2種方法都是很簡單的,參考網(wǎng)上的資料都不難做出,用io流做更是基礎(chǔ)中的基礎(chǔ),我說下smartupload好了,有的人是直接寫在jsp上面,感覺比較亂,我一般都是寫在action里面,打好jar包和配置后
SmartUpload mySmartUpload = new SmartUpload();
//如果是struts2.0或者webwork 則是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());
mySmartUpload.initialize(servlet.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(500000);
//如果上傳任意文件不設(shè)置mySmartUpload.setAllowedFilesList(文件后綴名)就可以了
mySmartUpload.upload();
for (int i = 0; i mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(保存的地址 + file.getFileName(),
su.SAVE_PHYSICAL);
本文題目:上傳商品java代碼 java實現(xiàn)上傳和下載
文章起源:http://www.dlmjj.cn/article/doohphh.html


咨詢
建站咨詢
