導航:首頁 > 編程語言 > java上傳控制項

java上傳控制項

發布時間:2023-08-23 11:20:23

java客戶端怎麼把本地的文件上傳到伺服器

String realpath = ServletActionContext.getServletContext().getRealPath("/upload") ;//獲取伺服器路徑
String[] targetFileName = uploadFileName;
for (int i = 0; i < upload.length; i++) {
File target = new File(realpath, targetFileName[i]);
FileUtils.File(upload[i], target);
//這是一個文件復制類File()裡面就是IO操作,如果你不用這個類也可以自己寫一個IO復制文件的類
}

其中private File[] upload;// 實際上傳文件

private String[] uploadContentType; // 文件的內容類型

private String[] uploadFileName; // 上傳文件名

這三個參數必須這樣命名,因為文件上傳控制項默認是封裝了這3個參數的,且在action裡面他們應有get,set方法

Ⅱ java實現圖片上傳至伺服器並顯示,如何做

給你段代碼,是用來在ie上顯示圖片的(servlet):

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
File file = new File(getServletContext().getRealPath("/")+"out"+"/"+id+".gif");
response.setCharacterEncoding("gb2312");
response.setContentType("doc");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"),"iso8859-1"));

System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));

OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(file);

byte[] b = new byte[1024];
int i = 0;

while((i = fis.read(b))!=-1)
{

output.write(b, 0, i);
}
output.write(b, 0, b.length);

output.flush();
response.flushBuffer();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}

}

這個程序的功能是根據傳入的文件名(id),來為瀏覽器返回圖片流,顯示在<img>標簽里
標簽的格式寫成如下:
<img src="http://localhost:8080/app/preview?id=111 "/><br/>
顯示的是111.gif這個圖片

你上面的問題:
1.我覺得你的第二個辦法是對的,我們也是這樣做的,需要的是把資料庫的記錄id號傳進servlet,然後讀取這條記錄中的路徑信息,生成流以後返回就是了

關於上傳文件的問題,我記得java中應該專門有個負責文件上傳的類,你調用就行了,上傳後存儲在指定的目錄里,以實體文件的形式存放
你可以參考這個:
http://blog.csdn.net/arielxp/archive/2004/09/28/119592.aspx

回復:
1.是的,在response中寫入流就行了
2.是發到servlet中的,我們一般都是寫成servlet,短小精悍,使用起來方便,struts應該也可以,只是我沒有試過,恩,你理解的很對

Ⅲ java編程:文件上傳,,普通上傳沒有控制項,怎麼限制他的大小啊,比如上傳文件不得超過10M,求大神

在後台action中判斷,把上傳的文件流用位元組讀出來,判斷這個文件的位元組流大小。然後根據自己的設置的最多進行判斷,1kb=1024b

閱讀全文

與java上傳控制項相關的資料

熱點內容
文電通pdfgold 瀏覽:853
上傳圖片的時候怎麼壓縮 瀏覽:31
千尋伺服器地址 瀏覽:812
ssh命令教程視頻 瀏覽:401
蘋果商店怎麼更新所有app 瀏覽:34
電影香港加密 瀏覽:595
壓縮機毛細管滴水 瀏覽:850
可視化編程網頁 瀏覽:389
工廠附近租房用什麼app 瀏覽:186
老闆改需求甩鍋程序員 瀏覽:657
樂貓app是什麼平台 瀏覽:922
方舟手游如何修改伺服器後台 瀏覽:815
程序員考試教程 瀏覽:219
文件名修改為文件夾的名字批處理 瀏覽:251
拍照程序員 瀏覽:827
wps怎麼把pdf轉jpg 瀏覽:217
自拍用什麼app做的藝術照 瀏覽:169
h3c無線配置命令 瀏覽:515
linux代碼閱讀工具 瀏覽:160
能夠畫出對稱圖形的是什麼app 瀏覽:424