① 怎麼用Apache搭建一個可以文件上傳下載的web網站
很簡單如果你有域名就綁定沒有直接使用IP進行訪問安裝php環境就可以了幫助直接訪問文件名字就可以下載的
② java web 大文件上傳下載
用框架 ssh
追問
如果大並發量,會不會出什麼問題?
回答
這個我沒試過,我只是上傳過照片。我記得這個是有上傳限制的,要在struts.xml文件中進行配置。
我的回答:
struts2支持多文件一起上傳 , 封裝性好。倘若文件大, 配置文件大小 可以搞定。上傳速度就卡你網速了。
③ javaweb 向指定ip上傳下載文件
這個其實用程序判斷,你把要制定的IP放倒資料庫中,通過程序判斷該IP是否符合。
我以前做過類似的程序,據我所知還沒有現成配置能達到這樣效果,除非在伺服器端做限制。
④ 怎樣使用javaweb實現上傳視頻和下載功能
文件上傳就是將客戶端資源,通過網路傳遞到伺服器端。
因為文件數據比較大,必須通過文件上傳才可以完成將數據保存到資料庫端的操作。
文件上傳的本質就是IO流操作。
演示:文件上傳應該如何操作?
瀏覽器端:
1.method=post 只有post才可以攜帶大數據
2.必須使用<input type='file' name='f'>要有name屬性
3.encType="multipart/form-data"
伺服器端:
request對象是用於獲取請求信息。
它有一個方法 getInputStream(); 可以獲取一個位元組輸入流,通過這個流,可以讀取到
所有的請求正文信息.
文件上傳原理:
瀏覽器端注意上述三件事,在伺服器端通過流將數據讀取到,在對數據進行解析.
將上傳文件內容得到,保存在伺服器端,就完成了文件上傳。
注意:在實際開發中,不需要我們進行數據解析,完成文件上傳。因為我們會使用文件上傳的工具,它們已經封裝好的,提供API,只要調用它們的API就可以完成文件上傳操作.我們使用的commons-fileupload,它是apache提供的一套開源免費的文件上傳工具。
代碼演示文件上傳的原理:
在WebRoot下新建upload1.jsp
[html]view plain
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>MyJSP'index.jsp'startingpage</title>
</head>
<body>
<!--encType默認是application/x-www-form-urlencoded-->
<formaction="${pageContext.request.contextPath}/upload1"
method="POST"enctype="multipart/form-data">
<inputtype="text"name="content"><br>
<inputtype="file"name="f"><br><inputtype="submit"
value="上傳">
</form>
</body>
</html>
packagecn.itcast.web.servlet;
importjava.io.IOException;
importjava.io.InputStream;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//System.out.println("upload1servlet......");
//通過request獲取一個位元組輸入流,將所有的請求正文信息讀取到,列印到控制台
InputStreamis=request.getInputStream();
byte[]b=newbyte[1024];
intlen=-1;
while((len=is.read(b))!=-1){
System.out.println(newString(b,0,len));
}
is.close();
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}
在web頁面中添加上傳輸入項。
在Servlet中讀取上傳文件的數據,並保存在伺服器硬碟中。
1、必須設置input輸入項的name屬性,否則瀏覽器將不會發送上傳文件的數據。
2、必須把form的encType屬性設為multipart/form-data 設置該值後,瀏覽器在上傳文件時,並把文件數據附帶在http請求消息體內,並使用MIME協議對上傳的文件進行描述,以方便接收方對上傳數據進行解析和處理。
3、表單的提交方式要設置為post。
Request對象提供了一個getInputStream方法,通過這個方法可以讀取到客戶端提交過來的數據。但由於用戶可能會同時上傳多個文件,在servlet端編程直接讀取上傳數據,並分別解析出相應的文件數據是一項非常麻煩的工作,示例。
為方便用戶處理文件上傳數據,Apache 開源組織提供了一個用來處理表單文件上傳的一個開源組件( Commons-fileupload ),該組件性能優異,並且其API使用極其簡單,可以讓開發人員輕松實現web文件上傳功能,因此在web開發中實現文件上傳功能,通常使用Commons-fileupload組件實現。
使用Commons-fileupload組件實現文件上傳,需要導入該組件相應支撐jar包:Commons-fileupload和commons-io。commo-io不屬於文件上傳組件的開發jar文件,但Commons-fileupload組件從1.1版本開始,它工作時需要commons-io包的支持。
新建Upload1Servlet 路徑:/upload1
[java]view plain
在瀏覽器端訪問信息如下:
文件上傳概述
實現web開發中的文件上傳功能,需要完成如下二步操作:
如何在web頁面中添加上傳輸入項?
<input type="file">標簽用於在web頁面中添加文件上傳輸入項,設置文件上傳輸入項時注意:
如何在Servlet中讀取文件上傳數據,並保存到本地硬碟中?
⑤ JAVA WEB項目文件夾上傳下載求思路
大概思路就是,前端將文件分片,然後每次訪問上傳介面的時候,向後端傳入參數:當前為第幾塊文件,和分片總數
⑥ Java Web項目實現上傳文件以及下載文件功能的關於路徑的問題
保存的框架是哪個框架,或者源碼是如何的
⑦ 騰訊雲部署的web系統能上傳不能下載文件
正常情況下,只要地址沒錯,而且如果tomcat能訪問到的話是可以下載的,你可以先測試一下能不能訪問tomcat的網頁,如果能打開他的頁面,再在ie地址欄中把地址改為那個文件所在的地址,試一下
⑧ 求JAVA WEB項目大文件上傳下載解決方案
使用minio,可以java也可以直接js。
⑨ 求ASP.NET WEB項目文件夾上傳下載解決方案
ASP.NET上傳文件用FileUpLoad就可以,但是對文件夾的操作卻不能用FileUpLoad來實現。
下面這個示例便是使用ASP.NET來實現上傳文件夾並對文件夾進行壓縮以及解壓。
ASP.NET頁面設計:TextBox和Button按鈕。
TextBox中需要自己受到輸入文件夾的路徑(包含文件夾),通過Button實現選擇文件夾的問題還沒有解決,暫時只能手動輸入。
兩種方法:生成rar和zip。
1.生成rar
using Microsoft.Win32;
using System.Diagnostics;
protected void Button1Click(object sender, EventArgs e)
{
RAR(@"E:95413594531GIS", "tmptest", @"E:95413594531");
}
///
///壓縮文件
///
///需要壓縮的文件夾或者單個文件
///生成壓縮文件的文件名
///生成壓縮文件保存路徑
///
protected bool RAR(string DFilePath, string DRARName,string DRARPath)
{
String therar;
RegistryKey theReg;
Object theObj;
String theInfo;
ProcessStartInfo theStartInfo;
Process theProcess;
try
{
theReg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRAR.exeShellOpenCommand"); //註:未在注冊表的根路徑找到此路徑
theObj = theReg.GetValue("");
therar = theObj.ToString();
theReg.Close();
therar = therar.Substring(1, therar.Length - 7);
theInfo = " a" + " " + DRARName + "" + DFilePath +" -ep1"; //命令 + 壓縮後文件名 + 被壓縮的文件或者路徑
theStartInfo = new ProcessStartInfo();
theStartInfo.FileName = therar;
theStartInfo.Arguments = theInfo;
theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
theStartInfo.WorkingDirectory = DRARPath ; //RaR文件的存放目錄。
theProcess = new Process();
theProcess.StartInfo = theStartInfo;
theProcess.Start();
theProcess.WaitForExit();
theProcess.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
///
///解壓縮到指定文件夾
///
///壓縮文件存在的目錄
///壓縮文件名稱
///解壓到文件夾
///
protected bool UnRAR(string RARFilePath,string RARFileName,string UnRARFilePath)
{
//解壓縮
String therar;
RegistryKey theReg;
Object theObj;
String theInfo;
ProcessStartInfo theStartInfo;
Process theProcess;
try
{
theReg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRar.exeShellOpenCommand");
theObj = theReg.GetValue("");
therar = theObj.ToString();
theReg.Close();
therar = therar.Substring(1, therar.Length - 7);
theInfo = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
theStartInfo = new ProcessStartInfo();
theStartInfo.FileName = therar;
theStartInfo.Arguments = theInfo;
theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
theProcess = new Process();
theProcess.StartInfo = theStartInfo;
theProcess.Start();
return true;
}
catch (Exception ex)
{
return false;
}
}
註:這種方法在在電腦注冊表中未找到應有的路徑,未實現,僅供參考。
2.生成zip
通過調用類庫ICSharpCode.SharpZipLib.dll
該類庫可以從網上下載。也可以從本鏈接下載:SharpZipLib_0860_Bin.zip
增加兩個類:Zip.cs和UnZip.cs
(1)Zip.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
namespace UpLoad
{
/// <summary>
///功能:壓縮文件
/// creator chaodongwang 2009-11-11
/// </summary>
public class Zip
{
/// <summary>
///壓縮單個文件
/// </summary>
/// <param name="FileToZip">被壓縮的文件名稱(包含文件路徑)</param>
/// <param name="ZipedFile">壓縮後的文件名稱(包含文件路徑)</param>
/// <param name="CompressionLevel">壓縮率0(無壓縮)-9(壓縮率最高)</param>
/// <param name="BlockSize">緩存大小</param>
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel)
{
//如果文件沒有找到,則報錯
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("文件:" + FileToZip + "沒有找到!");
}
if (ZipedFile == string.Empty)
{
ZipedFile = Path.GetFileNameWithoutExtension(FileToZip) + ".zip";
}
if (Path.GetExtension(ZipedFile) != ".zip")
{
ZipedFile = ZipedFile + ".zip";
}
////如果指定位置目錄不存在,創建該目錄
//string zipedDir = ZipedFile.Substring(0,ZipedFile.LastIndexOf("\"));
//if (!Directory.Exists(zipedDir))
//Directory.CreateDirectory(zipedDir);
//被壓縮文件名稱
string filename = FileToZip.Substring(FileToZip.LastIndexOf('\') + 1);
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry(filename);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[2048];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
}
/// <summary>
///壓縮文件夾的方法
/// </summary>
public void ZipDir(string DirToZip, string ZipedFile, int CompressionLevel)
{
//壓縮文件為空時默認與壓縮文件夾同一級目錄
if (ZipedFile == string.Empty)
{
ZipedFile = DirToZip.Substring(DirToZip.LastIndexOf("\") + 1);
ZipedFile = DirToZip.Substring(0, DirToZip.LastIndexOf("\")) +"\"+ ZipedFile+".zip";
}
if (Path.GetExtension(ZipedFile) != ".zip")
{
ZipedFile = ZipedFile + ".zip";
}
using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(ZipedFile)))
{
zipoutputstream.SetLevel(CompressionLevel);
Crc32 crc = new Crc32();
Hashtable fileList = getAllFies(DirToZip);
foreach (DictionaryEntry item in fileList)
{
FileStream fs = File.OpenRead(item.Key.ToString());
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(DirToZip.Length + 1));
entry.DateTime = (DateTime)item.Value;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
}
}
/// <summary>
///獲取所有文件
/// </summary>
/// <returns></returns>
private Hashtable getAllFies(string dir)
{
Hashtable FilesList = new Hashtable();
DirectoryInfo fileDire = new DirectoryInfo(dir);
if (!fileDire.Exists)
{
throw new System.IO.FileNotFoundException("目錄:" + fileDire.FullName + "沒有找到!");
}
this.getAllDirFiles(fileDire, FilesList);
this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);
return FilesList;
}
/// <summary>
///獲取一個文件夾下的所有文件夾里的文件
/// </summary>
/// <param name="dirs"></param>
/// <param name="filesList"></param>
private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)
{
foreach (DirectoryInfo dir in dirs)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
this.getAllDirsFiles(dir.GetDirectories(), filesList);
}
}
/// <summary>
///獲取一個文件夾下的文件
/// </summary>
/// <param name="strDirName">目錄名稱</param>
/// <param name="filesList">文件列表HastTable</param>
private void getAllDirFiles(DirectoryInfo dir, Hashtable filesList)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
}
}
}
(2)UnZip.cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///解壓文件
/// </summary>
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
namespace UpLoad
{
/// <summary>
///功能:解壓文件
/// creator chaodongwang 2009-11-11
/// </summary>
public class UnZipClass
{
/// <summary>
///功能:解壓zip格式的文件。
/// </summary>
/// <param name="zipFilePath">壓縮文件路徑</param>
/// <param name="unZipDir">解壓文件存放路徑,為空時默認與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</param>
/// <param name="err">出錯信息</param>
/// <returns>解壓是否成功</returns>
public void UnZip(string zipFilePath, string unZipDir)
{
if (zipFilePath == string.Empty)
{
throw new Exception("壓縮文件不能為空!");
}
if (!File.Exists(zipFilePath))
{
throw new System.IO.FileNotFoundException("壓縮文件不存在!");
}
//解壓文件夾為空時默認與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\"))
unZipDir += "\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\"))
directoryName += "\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
}
}
以上這兩個類庫可以直接在程序里新建類庫,然後復制粘貼,直接調用即可。
主程序代碼如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Microsoft.Win32;
using System.Diagnostics;
namespace UpLoad
{
public partial class UpLoadForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "") //如果輸入為空,則彈出提示
{
this.Response.Write("<script>alert('輸入為空,請重新輸入!');window.opener.location.href=window.opener.location.href;</script>");
}
else
{
//壓縮文件夾
string zipPath = TextBox1.Text.Trim(); //獲取將要壓縮的路徑(包括文件夾)
string zipedPath = @"c: emp"; //壓縮文件夾的路徑(包括文件夾)
Zip Zc = new Zip();
Zc.ZipDir(zipPath, zipedPath, 6);
this.Response.Write("<script>alert('壓縮成功!');window.opener.location.href=window.opener.location.href;</script>");
//解壓文件夾
UnZipClass unZip = new UnZipClass();
unZip.UnZip(zipedPath+ ".zip", @"c: emp"); //要解壓文件夾的路徑(包括文件名)和解壓路徑(temp文件夾下的文件就是輸入路徑文件夾下的文件)
this.Response.Write("<script>alert('解壓成功!');window.opener.location.href=window.opener.location.href;</script>");
}
}
}
}
本方法經過測試,均已實現。
另外,附上另外一種上傳文件方法,經測試已實現,參考鏈接:http://blog.ncmem.com/wordpress/2019/11/20/net%e4%b8%8a%e4%bc%a0%e5%a4%a7%e6%96%87%e4%bb%b6%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88/
⑩ JAVA WEB項目大文件上傳下載求思路
現在大多提供填報功能的報表工具都會提供上傳下載組件,可以直接使用報表工具做張填報表添加這樣的控制項就可以實現這樣的功能。
具體實現可以參考下:如何實現文件(附件)的上下載