我有以下代码可以处理服务器上的文件上传。但是如何将文件保存到服务器上的特定位置
import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;
import org.apache.commons.fileupload.FileItem;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This is an example of how to use UploadAction class.
*
* This servlet saves all received files in a temporary folder,
* and deletes them when the user sends a remove request.
*
* @author Manolo Carrasco Moñino
*
*/
public class SampleUploadServlet extends UploadAction {
private static final long serialVersionUID = 1L;
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
/**
* Maintain a list with received files and their content types.
*/
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
/**
* Override executeAction to save the received files in a custom place
* and delete this items from session.
*/
@Override
public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont++;
try {
/// Create a new file based on the remote file name in the client
// String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
// File file =new File("/tmp/" + saveName);
/// Create a temporary file placed in /tmp (only works in unix)
// File file = File.createTempFile("upload-", ".bin", new File("/tmp"));
/// Create a temporary file placed in the default system temp folder
File file = File.createTempFile("upload-", ".bin");
item.write(file);
/// Save a list with the received files
receivedFiles.put(item.getFieldName(), file);
receivedContentTypes.put(item.getFieldName(), item.getContentType());
/// Compose a xml message with the full file information
response += "<file-" + cont + "-field>" + item.getFieldName() + "</file-" + cont + "-field>\n";
response += "<file-" + cont + "-name>" + item.getName() + "</file-" + cont + "-name>\n";
response += "<file-" + cont + "-size>" + item.getSize() + "</file-" + cont + "-size>\n";
response += "<file-" + cont + "-type>" + item.getContentType() + "</file-" + cont + "type>\n";
}
catch (Exception e)
{
}
}
}
/// Remove files from session because we have a copy of them
removeSessionFileItems(request);
/// Send information of the received files to the client.
return "<response>\n" + response + "</response>\n";
}
/**
* Get the content of an uploaded file.
*/
@Override
public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String fieldName = request.getParameter(PARAM_SHOW);
File f = receivedFiles.get(fieldName);
if (f != null) {
response.setContentType(receivedContentTypes.get(fieldName));
FileInputStream is = new FileInputStream(f);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND);
}
}
/**
* Remove a file when the user sends a delete request.
*/
@Override
public void removeItem(HttpServletRequest request, String fieldName) throws UploadActionException {
File file = receivedFiles.get(fieldName);
receivedFiles.remove(fieldName);
receivedContentTypes.remove(fieldName);
if (file != null) {
file.delete();
}
}
}
您应该使用File#createTempFile()
which代替一个目录。
File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
item.write(file);
或者,如果你真的想临时文件移动到另一个位置 之后
,使用File#renameTo()
。
File destination = new File("/path/to/your/uploads", file.getName());
file.renameTo(destination);
我的项目是一个web应用程序。 目前,它有一种生成文本文件并将其发送到客户端浏览器下载的方法。 我需要修改它,以强制浏览器将文件保存在客户端计算机上的自定义(预定义)位置。
问题 上传文件,并将其保存到预先设定的某个目录下。 方法 import web urls = ('/upload', 'Upload') class Upload: def GET(self): web.header("Content-Type","text/html; charset=utf-8") return """<html><head></he
我想上传文件并保存到特定的目录。而且我对文件的概念是陌生的。当我从我的页面上传文件时,它们保存在另一个目录(C:\ Users \ ROOTCP ~ 1 \ AppData \ Local \ Temp \ multipartbody 989135345617811478 astemporary file)而不是指定的目录。我不能设置它。请帮我找到解决办法。提前感谢所有的帮助。 上传的文件为什么保
问题内容: 我一直试图将纯文本文件保存到Android上Google云端硬盘中的特定文件夹中。 到目前为止,使用Google云端硬盘上的文档和快速入门指南,我已经可以做一些正确的事情,首先,我可以创建一个纯文本文件: 我已经能够使用以下命令在Google云端硬盘的基本目录中创建一个新文件夹: 我还能够通过以下方式列出用户的Google云端硬盘帐户中的所有文件夹: 但是,我对如何将文本文件保存到Go
我想将一个文件保存在与应用程序jar文件所在位置相同的文件夹中。我正试图通过以下方式实现这一点: 如果我从Intellij运行项目,返回的路径是,如果我从命令行运行它,返回的路径是。这是应该发生的吗?另外,如何才能将jar路径仅返回到?