我正在处理有关 MultipartFile 的奇怪行为。
我的项目是一个接收文本文件的 Spring Boot 后端。该文本文件作为 MultipartFile 出现。我想将此文件发送到辅助 Spring Boot 后端,该后端将在我的主后端读取该文件之前向该文件添加一些内容。这些内容更改不是强制性的,如果它们不存在,程序也不会崩溃。
要将 MultipartFile 发送到另一个后端,我必须将 MultipartFile 转换为 java.io.File。在执行此操作时,MultipartFile 会被破坏。
创建 java.io.File 后,BufferedReader 无法读取原始 MultipartFile。
重度编辑:
我的项目规格发生了变化,额外的后端被取消了。不过我仍然很好奇这里发生了什么。以下代码重现了我遇到的异常:
@CrossOrigin
@RestController
@RequestMapping("/dragon")
public class TestController {
@PostMapping("/killFile")
public String sendInFileHere(@Valid @RequestBody MultipartFile multipartFile) {
if (multipartFile == null) {
throw new IllegalArgumentException("File has to be Present");
}
File file = new File(multipartFile.getOriginalFilename());
try {
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader;
try {
InputStream is = multipartFile.getInputStream(); //exception is thrown …Run Code Online (Sandbox Code Playgroud) 我得到以下IOException:
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
at zipUnzipper.main(zipUnzipper.java:41)
Run Code Online (Sandbox Code Playgroud)
尝试运行以下代码时:
public class zipUnzipper {
public zipUnzipper() {
}
public static void main(String[] args){
//Unzip to temp folder. Add all files to mFiles. Print names of all files in mFfiles.
File file = new File("C:\\aZipFile.zip");
String filename = file.getName();
String filePathName = new String();
int o = filename.lastIndexOf('.');
filename = filename.substring(0,o);
try {
ZipFile zipFile = new ZipFile (file.getAbsoluteFile());
Enumeration entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry zipEntry = …Run Code Online (Sandbox Code Playgroud) 当我停在这行代码上时,我正在查看我的代码,以便最后一次搜索异常:
var list: Array[String] = Source.fromFile(this.Path).getLines.toArray
Run Code Online (Sandbox Code Playgroud)
我搜索了scala-lang的文档,但似乎没有任何一种方法抛出任何一种ioException......这怎么可能?
编辑:
try {
var list: Array[String] = Source.fromFile("").getLines.toArray
}
catch {
case ex:Exception => println(ex.getMessage)
}
Run Code Online (Sandbox Code Playgroud)
什么都不打印?
我正在建立与服务器的连接及其正常工作.但有时我尝试获取响应代码(当服务器正在抛回401,403,404时)我得到IO异常.我根据响应代码处理所有内容.所以当它抛出IO异常时
http.getResponseCode()
Run Code Online (Sandbox Code Playgroud)
我无法阅读响应代码.请为此建议一个解决方案.
如果我尝试使用没有完全限定名称的IOException并且没有导入,则会出现编译错误.但是,当我使用RuntimeException或Exception执行相同操作时,不会发生这种情况.这是为什么?
谢谢
在我的应用程序中,我试图使用保存BufferedImage到PNG文件ImageIO.该文件由用户选择,因此我需要对可能发生的错误做出反应(例如,用户试图保存在他没有写入权限的位置).但是我无法IOException理解这种情况.
以下代码显示了该问题.尝试保存到"/ foo"应该为*nix系统上的大多数用户抛出异常,因为它们在根目录中没有写入权限.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class IOTest {
public static void main(String[] args) {
BufferedImage img = new BufferedImage(640, 480,
BufferedImage.TYPE_INT_RGB);
try {
File f = new File("/foo");
ImageIO.write(img, "png", f);
} catch (IOException e) {
System.out.println("Caught IOException!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,没有捕获异常.输出:
java.io.FileNotFoundException: /foo (Permission denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:233)
at javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:69)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:55)
at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419)
at javax.imageio.ImageIO.write(ImageIO.java:1530)
at IOTest.main(IOTest.java:16)
Exception in thread "main" java.lang.NullPointerException
at …Run Code Online (Sandbox Code Playgroud) 我正在使用标准API程序连接到设备.特别是OBDII蓝牙适配器.
我有两个不同型号的OBDII蓝牙适配器.运行完全相同的代码,第一个将与我测试的每个手机/平板电脑配对和连接没有问题.第二个将配对和连接正常,除了我的运行Android 4.2的Nexus 7
它抛出一个IOexception: read failed, socket might closed or timeout, read ret: -1
认为这只是设备本身或更新版本的错误...但是另一个软件"扭矩"能够从我的Nexus 7连接到任一适配器.
所以我显然做了一些错误/不同的事情,这只是后来的OS中的一个问题???
任何帮助搞清楚这一点将不胜感激.
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect(); <=**This is where the exception is thrown
} catch (IOException e) {
Etype …Run Code Online (Sandbox Code Playgroud) 有什么方法可以从快捷方式打开记事本或其他应用程序吗?
这是我的代码:
import java.io.File;
import java.io.IOException;
public class acrobat {
public static void main(String[] args) throws IOException, InterruptedException {
String[] notepad = {"C:\\Users\\Desktop\\notepad.lnk"};
Process p = Runtime.getRuntime().exec(notepad);
p.waitFor();
}
}
Run Code Online (Sandbox Code Playgroud)
我想从快捷方式打开应用程序,但我收到错误..
Exception in thread "main" java.io.IOException: Cannot run program "C:\Users\robert\Desktop\notepad.lnk": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at acrobat.main(acrobat.java:11)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown …Run Code Online (Sandbox Code Playgroud) 以下代码在Windows 2008上失败了.它在Win7中成功.
return new Semaphore(1, 1, "my-test-semaphore");
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
System.IO.IOException: The specified port does not exist.
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
at System.Threading.Semaphore..ctor(Int32 initialCount, Int32 maximumCount, String name)
at Throttle.Program.CreateSemaphore(String passthroughApplication)
at Throttle.Program.Main(String[] args)
Run Code Online (Sandbox Code Playgroud)
我四处搜寻,找不到解释.有没有人有任何想法?
我在应用程序中使用WritingMinds/ffmpeg-android-java.
这是我的代码
loadFFmpeg();
String cmd="ffmpeg -i /storage/emulated/0/media/audio/a.mp3 -i /storage/emulated/0/recording.3gp -filter_complex \"[0:a][1:a]amerge=inputs=2[aout]\" -map \"[aout]\" " + outputFile;
executeFFmpeg(cmd.split(" "));
Run Code Online (Sandbox Code Playgroud)
和
private void loadFFmpeg() {
FFmpeg ffmpeg = FFmpeg.getInstance(MainActivity.this.getApplicationContext());
try {
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override
public void onStart() {}
@Override
public void onFailure() {}
@Override
public void onSuccess() {}
@Override
public void onFinish() {}
});
} catch (FFmpegNotSupportedException e) {
// Handle if FFmpeg is not supported by device
}
}
private void executeFFmpeg(String[] cmd)
{
/*String workFolder = getApplicationContext().getFilesDir() …Run Code Online (Sandbox Code Playgroud)