protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String fileName = req.getParameter("fileName");
String filePath = "c:/file folder/... ";
File file = new File(filePath);
int length = 0;
ServletOutputStream out = resp.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(filePath);
if (mimetype == null) {
mimetype = "application/octet-stream";
}
resp.setContentType(mimetype);
int fileSize = (int) file.length()
if (fileSize == 0) {
resp.setContentLength(fileSize);
}else {
// some kind of file with empty content, if you dont specify the length, it will simply return a unkown file
resp.setContentLength(1);
}
//
resp.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
byte[] byteBuffer = new byte[BUFFER_SIZE];
DataInputStream in = new DataInputStream(new FileInputStream(file));
while ((in != null) && ((length = in.read(byteBuffer)) != -1)) {
out.write(byteBuffer, 0, length);
}
in.close();
out.flush();
out.close();
}
分享到:
相关推荐
在Java中实现WebSocket,我们可以借助于Tomcat 8这样的Servlet容器以及JDK 1.8提供的API。下面将详细介绍WebSocket在Java中的实现及其与Tomcat 8和JDK 1.8的结合使用。 首先,我们需要了解WebSocket的基本概念。...
$("a[id=downLoad]").click(function() { window.location.href = path.deployPath + "/fileDown"; }); ``` 服务器端发送文件 在服务器端,需要使用JavaServlet来发送文件。可以使用Java的inputStream和...
function downloadData(data, filename) { var blob = new Blob([data], {type: 'application/octet-stream'}); var url = URL.createObjectURL(blob); var link = document.createElement('a'); link.href = ...
@WebServlet("/download") public class DownloadServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ...
changeFile: function(event) { this.file = event.target.files[0]; var pos = $("#path").val().lastIndexOf("\\"); var fileName = $("#path").val().substring(pos + 1); document.getElementById("excel-...
File uploadedFile = new File("/path/to/save/files/" + fileName); item.write(uploadedFile); } } // 保存文件信息到数据库或其他操作 return mapping.findForward("success"); } ``` 下载部分,我们...
<a TARGET="#" href="<%=path%>/common/file/download.action?fileId=<ww:property value="%{id}" />"> <span style="width:10%"> <img align="middle" onclick="removeFile(this,'...
这可以通过访问[Apache Log4j官网](https://logging.apache.org/log4j/2.x/download.html)下载相应的JAR包。 3. **配置Log4j属性文件**: 在项目的资源文件夹中创建或复制一个`log4j.properties`文件,内容如下: `...
<input type="file" name="file" /> 上传 $(document).ready(function() { $("#uploadForm").on("submit", function(event) { event.preventDefault(); var formData = new FormData(this); $.ajax({ url:...
for (File file : fileList) { // 下载文件 InputStream in = new FileInputStream(file); // 添加到zip文件中 zipStream.putNextEntry(new ZipEntry(file.getName())); IOUtils.copy(in, zipStream); in....
document.getElementById('fileInput').addEventListener('change', function(event) { var files = event.target.files; // 处理files对象,如上传到服务器 }); ``` 接下来,我们需要处理文件上传。在...
public StreamingResponseBody download(@RequestParam("userfile1") MultipartFile file1, @RequestParam("userfile2") MultipartFile file2) { // 检查文件是否为空,大小等 if (file1.isEmpty() || file2....
The file struts-config.xml instructs ActionServlet on how to use the extended classes. There are several advantages to this approach: • The entire logical flow of the application is in a ...
import javax.servlet.http.HttpServletRequest; import org.springframework.web.multipart.MultipartFile; public class FileUploadAction { public void handleUpload(HttpServletRequest request) { ...
1. 直接下载:通过 `FileReference` 类的 `download()` 方法,可以从 URL 下载文件到本地。用户可以选择保存文件的位置,或者让系统自动保存。 2. 浏览器下载:另一种方式是创建一个指向下载链接的 `URLRequest`,...
2. **文件下载**:Servlet监听/download路径的请求,根据请求参数找到对应的文件,然后返回文件内容,浏览器将其作为下载处理。 3. **图片浏览**:用户可以在页面上查看所有图片的列表,点击链接可以跳转到图片...
Adapting a File-like Object to a True File Object Recipe 2.16. Walking Directory Trees Recipe 2.17. Swapping One File Extension for Another Throughout a Directory Tree Recipe 2.18. Finding a ...
`jQuery.uploadify` 插件可以从其官方网站(http://www.uploadify.com/download/)下载。这个插件简化了图片上传的实现,提供了一整套可配置的选项和事件处理机制,使得在网页应用中集成图片上传功能变得更加便捷。
function getFile(){ // 不推荐使用$.get进行文件下载 // $.get("<%=basePath%>LoadFileServlet","",function(data){ alert(data); }); } // 更推荐的做法是使用超链接直接触发下载 <a href="<%=basePath%>...